Thursday, September 20, 2007

.NET HttpWebRequest : Sending content exceeds 1024 bytes

When we use HttpWebRequest in .NET to send an HTTP request with some content being attached, we may use, code something like the following.

httpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(serviceUri);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/xml";
httpWebRequest.ContentLength = steRequest.Length;

writer = new StreamWriter(httpWebRequest.GetRequestStream());
writer.Write(steRequest);
writer.Flush();
writer.Close();

This has some limitations. In this case, steRequest is a string, which contains the content which is to be attached to the HTTP request. See what happens, if the the length of steRequest exceeds 1024 - you won't get the rest attached.

Following is the work-around for that.

Remove the line; httpWebRequest.ContentLength = steRequest.Length;

Add the following instead; httpWebRequest.AllowWriteStreamBuffering = true;

No comments:

Post a Comment