user1231231412
user1231231412

Reputation: 1659

Encoding issue with HttpWebResponse

Running the below code with Fiddler shows that plus signs are getting converted to a white space char of some sort. What encoding should I use to keep the data from being converted? I would like to keep the plus signs, etc.

EDIT: Updated code example

string postData = "test1=test+plussign&test2=another++twoplussigns";
private static byte[] EncodePostData(string postData)
{
    StringBuilder sb = new StringBuilder();

    string[] pairs = postData.Split('&');
    foreach (string pair in pairs)
    {
        string key = Uri.EscapeDataString(pair.Split('=')[0]);
        string value = Uri.EscapeDataString(pair.Split('=')[1]);

        sb.AppendFormat("{0}={1}&",key, value);
    }
    sb.Remove(sb.Length - 1, 1);

    return HttpUtility.UrlEncodeToBytes(sb.ToString());
}

And here is the calling method

byte[] data = EncodePostData(postData);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.03";
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
ServicePointManager.Expect100Continue = false;

Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();

HttpWebResponse Response = (HttpWebResponse)request.GetResponse();
Response.Close();

Upvotes: 1

Views: 1760

Answers (3)

sbtech100
sbtech100

Reputation: 59

Try this:

string postData =  Uri.EscapeDataString("test+PlusSigns");
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData);
writer.Close();

HttpWebResponse Response = (HttpWebResponse)request.GetResponse();
Response.Close();

Upvotes: 1

Bradley Grainger
Bradley Grainger

Reputation: 28207

Since your variable is called postData, I'm going to assume you're making a HTTP POST to a web server.

Wikipedia's summary of the rules for HTTP POST is:

When a web browser sends a POST request from a web form element, the default Internet media type is "application/x-www-form-urlencoded". This is a format for encoding key-value pairs with possibly duplicate keys. Each key-value pair is separated by an '&' character, and each key is separated from its value by an '=' character. Keys and values are both escaped by replacing spaces with the '+' character and then using URL encoding on all other non-alphanumeric characters.

That is, the default interpretation of '+' in a HTTP POST body is an encoded ' ' character. You need to escape your data properly, by calling Uri.EscapeDataString or HttpUtility.UrlEncode.

Uri.EscapeDataString("test+PlusSigns"); // returns "test%2BPlusSigns"

Alternatively, use HttpUtility.UrlEncodeToBytes and write the resulting bytes directly to the request stream (without using a StreamWriter).

(Note that if you are sending key/value pairs, you need to escape each key and value independently, then join them with '=' and '&' characters.)

Upvotes: 2

Anand
Anand

Reputation: 14955

The maximum you could do is set the request content type

myReq.ContentType = "text/html; charset=UTF-8";

Upvotes: -1

Related Questions