Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# HttpWebRequest POST'ing failing

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?

like image 228
KJ Tsanaktsidis Avatar asked Nov 01 '09 09:11

KJ Tsanaktsidis


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


2 Answers

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);
    }

}
like image 91
Jon Skeet Avatar answered Oct 08 '22 04:10

Jon Skeet


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
 });
like image 39
Marc Gravell Avatar answered Oct 08 '22 02:10

Marc Gravell