100 Character Limit on Accepting Payload of HTTP POST in C# Service?

Posted on 2009-02-11
Last Modified: 2013-11-07

I have a WCF Service that takes the payload of an HTTP POST and saves it as a string. However, it only seems to work when the payload is 99 characters or less. For example, I will make the payload 99 a's and it works. But, I add on one more 'a' and suddenly there is no response!

Is there some reason for this, and more importantly, how can I make my program work for the huge payloads I am aiming for? I have included below my entire .cs file (there is no app config file). FYI: I am HTTP POSTing to the WCF Service with a C program running in a linux box (I will also include that file in case you need it)

Any help would be greatly appreciated! Thanks!
======================WCF Service============================ 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.ServiceModel; 

using System.ServiceModel.Activation; 

using System.ServiceModel.Description; 

using System.ServiceModel.Dispatcher; 

using System.ServiceModel.Channels; 

using System.Xml; 

using System.IO; 

using System.Web; 

using System.Text; 

using System.Runtime.Serialization; 

using System.ServiceModel.Web; 

using System.Net; 

using System.Collections.Specialized; 

using System.Diagnostics; 


public class getString 



        public interface ITest   




            void SaveString(Stream stream);   


        public class Service : ITest   


            public void SaveString(Stream text) 


                string stringText = new StreamReader(text).ReadToEnd().Replace('*','\n'); 

                string now = DateTime.Now.ToString(); 

                Console.WriteLine("\n{0}: contact made...", now); 


                string nowForFile = DateTime.Now.ToString().Replace('/', '_').Replace(':', '_'); 

                string fileName = "Result_" + nowForFile + ".xml"; 

                StreamWriter sw = new StreamWriter(fileName); 





                Console.WriteLine("xml payload saved as file...\n\n"); 



        static Binding GetBinding()   


            BasicHttpBinding result = new BasicHttpBinding(); 

            return result;   



    public static void Main() 


        string baseAddress = ""; 

        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress)); 


        Console.WriteLine("Serivce running at {0}", baseAddress); 

        WebClient client = new WebClient(); 

        client.Headers[HttpRequestHeader.ContentType] = "text/plain"; 

        Console.WriteLine("Press <ENTER> To Exit...\n\n"); 












=======================post client c program=================== 


/* http_post_simple - do a POST to an HTTP URL and return the values

   Copyright GPL 2003 by Mike Chirico <>

   Updated: Sun Jun 13 13:53:14 EDT 2004 */ 


/* Edited and customized by Erik Hensens */ 


#include <arpa/inet.h> 

#include <assert.h> 

#include <errno.h> 

#include <netinet/in.h> 

#include <signal.h> 

#include <stdlib.h> 

#include <stdio.h> 

#include <string.h> 

#include <sys/types.h> 

#include <sys/socket.h> 

#include <sys/wait.h> 

#include <netdb.h> 

#include <unistd.h> 


#define SA struct sockaddr 

#define MAXLINE 4096 

#define MAXSUB 200 

#define LISTENQ 1024 

#define host "" 

#define PORT 8004 


extern int h_errno; 


ssize_t processHttp(int sockfd) 




    /* The limit seems to be 99 characters... :( */ 

    /* Define string to send here */ 

    char xmlString[] = 





    char sendline[MAXLINE + 1], recvline[MAXLINE + 1]; 

    ssize_t n; 


    snprintf(sendline, MAXSUB, 


        /* Actual HTTP POST text */ 

        "POST /Service/SaveString HTTP/1.0\r\n" 

        "Host: %s\r\n" 

        "Content-type: text/plain\r\n" 

        "Content-length: %d\r\n\r\n" 

        "%s", host, ((sizeof(xmlString)) - 1), xmlString); 


    write(sockfd, sendline, strlen(sendline)); 


    /* Exit program before receiving HTTP Response */ 



    /* Receive HTTP Response */ 

    while ((n = read(sockfd, recvline, MAXLINE)) > 0) 


        recvline[n] = '\0'; 

        printf("%s", recvline); 



    return n; 



int main(void) 


    int sockfd; 

    struct sockaddr_in servaddr; 

    char **pptr; 

    char str[50]; 

    struct hostent *hptr; 


    hptr = gethostbyname(host); 

    if (hptr == NULL) 


        fprintf(stderr, " gethostbyname error for host: %s: %s", host, hstrerror(h_errno)); 




    printf("\nhostname: %s\n", hptr->h_name); 


    if (hptr->h_addrtype == AF_INET && (pptr = hptr->h_addr_list) != NULL) 

        printf("address: %s", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str))); 


        fprintf(stderr, "Error call inet_ntop\n"); 




    sockfd = socket(AF_INET, SOCK_STREAM, 0); 

    bzero(&servaddr, sizeof(servaddr)); 

    servaddr.sin_family = AF_INET; 

    servaddr.sin_port = htons(PORT); 

    inet_pton(AF_INET, str, &servaddr.sin_addr); 


    connect(sockfd, (SA *) & servaddr, sizeof(servaddr)); 





Open in new window

Question by:ehensens
    LVL 4

    Expert Comment

    I wouldn't even think you could get 99 characters into a char. It looks like when you use emtpy array definition [] that it uses 100 as the default. Try specifying the size of your array with char xmlString[200] or whatever size you need. I didn't look at your code much, but perhaps you should be using a string instead. Usually, I would use a fixed size char array but use it as a buffer to read the input.

    Author Comment


    Thank you very much for your input. On your advice I tried specifying the size of my character array as something larger than 100 but the problem still persists somehow.

    Regarding using a variable of type 'string' instead, I would absolutely love to, but you'll notice it is written in C and C of course has no string variable type. And, changing to C++ or something else is not a possibility.

    Also, what did you mean exactly by "use a fixed size char array but use it as a buffer to read the input"?

    Thank you!
    LVL 4

    Expert Comment

    I'm sorry, I hadn't looked at the rest of your code. In your title, you said that it was C#. I see now that your server is C#, and your client is in C. This appears to likely be a stream blocking issue. Using ReadToEnd doesn't work well on network streams. Check out this method.

    Author Comment

    Thank you very much, this looks promising. However you clearly know more about this than I do, do you have any hints on how to incorporate this into my program or would this require a complete overhaul?

    Accepted Solution

    I found out what the deal was. It was not in my WCF, it was in my C program. The second argument of snprintf, "MAXSUB" was set at 200 and I just increased it and there we go.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now