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


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 <mchirico@users.sourceforge.net>
   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

Who is Participating?
ehensensAuthor Commented:
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.
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.
ehensensAuthor Commented:

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!
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.

ehensensAuthor Commented:
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.