views:

659

answers:

3

So i'm trying to POST something to a webserver.

System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("url");
System.String Content = "id=" + Id;
EventReq.ContentLength = System.Text.Encoding.UTF8.GetByteCount(Content);
EventReq.Method = "POST";
EventReq.ContentType = "application/x-www-form-urlencoded";
System.IO.StreamWriter sw = new System.IO.StreamWriter(EventReq.GetRequestStream(), System.Text.Encoding.UTF8);
sw.Write(Content);
sw.Flush();
sw.Close();

Looks alright, i'm setting content-length based on the size of the ENCODED data... Anyway it fails at sw.flush() with "bytes to be written to the stream exceed the Content-Length size specified"

Is StreamWriter doing some magic behind my back i'm not aware of? Is there a way i can peer into what StreamWriter is doing?

+2  A: 

Maybe make like easier:

using(WebClient client = new WebClient()) {
    NameValueCollection values = new NameValueCollection();
    values.Add("id",Id);
    byte[] resp = client.UploadValues("url","POST", values);
}

Or see here for a discussion allowing use like:

client.Post(destUri, new {
     id = Id // other values here
 });
Marc Gravell
A: 

You need not set ContentLength explicitly, since it will be set automatically to the size of data written to request stream when you close it.

elder_george
You are indeed correct; this fixes that particular problem. But using Wireshark to look at the packet that gets sent shows that StreamWriter is prepending something to the POST data...\357\273\277id=301RbuWhere are those 3 bytes coming from??? Content-Length is set to 12 in this packet.
KJ Tsanaktsidis
@Jon Skeet described this well (as usual =)) - this is so-called `Unicode byte-order mark` that identifies whether text is UTF-8, UTF-16 Big-endian, UTF-16 Little-endian etc. Read more here http://en.wikipedia.org/wiki/Byte-order_mark
elder_george
+3  A: 

Other answers have explained how to avoid this, but I thought I'd answer why it's happening: you're ending up with a byte order mark before your actual content.

You can avoid this by calling new UTF8Encoding(false) instead of using Encoding.UTF8. Here's a short program to demonstrate the difference:

using System;
using System.Text;
using System.IO;

class Test    
{
    static void Main()
    {
        Encoding enc = new UTF8Encoding(false); // Prints 1 1
        // Encoding enc = Encoding.UTF8; // Prints 1 4
        string content = "x";
        Console.WriteLine(enc.GetByteCount("x"));
        MemoryStream ms = new MemoryStream();
        StreamWriter sw = new StreamWriter(ms, enc);
        sw.Write(content);
        sw.Flush();
        Console.WriteLine(ms.Length);
    }

}
Jon Skeet
Good spot. I *thought* about digging, but... <g>
Marc Gravell
You sir are correct :)Explains that observation about \357\273\277 from wireshark i made in the other comment.Many thanks!
KJ Tsanaktsidis