ContentLength trouble!

W

woof.rana

Hi,

I've been struggling with this POST problem for more than three hours
now, and don't see any light.
Below is the troublesome code snippet:


System.Net.HttpWebRequest req =
HttpWebRequest)System.Net.WebRequest.Create(URI);
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.Method="POST";
req.SendChunked=true;
req.ContentType = @"application/x-www-form-urlencoded";
req.ContentLength = bytes.Length;
req.Headers.Add("Accept-Encoding: gzip, deflate");

System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
Console.WriteLine("The value of 'ContentLength' property after sending
the data is {0}", req.ContentLength);
os.Close();

Now, when I send the request:
System.Net.HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
it gives me an exception: 411 Length required.

I know this means that the Content-Length header needs to be set. the
WriteLine gives me a valid number, 561, and checking with Fiddler2
shows no Content-Length header. In fact, checking req.Headers.AllKeys
shows no Content-Length header, even after I set it.
I cannot even do:
req.Headers.Add("Content-Length: " + bytes.Length.ToString());
because it throws an ArgumentException saying that this header should
be set by the appropriate property.
What is going on completely escapes me!

Any help much appreciated!

Rana
 
S

steveS

Hi Rana,
Try amending your code to the following
os.Write(bytes, 0, bytes.Length);
os.Flush();

Still having problems let me know.
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top