I’m trying to send a base64 encoded data using a POST request and it contains the “+” char. When I send the request, the “+” is replaced with ” ” (space). Here goes the code
public string POST(string url, string query)
{
HttpWebRequest hwrq = CreateRequest(url);
hwrq.CookieContainer = Cookies;
hwrq.Method = "POST";
hwrq.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.Default.GetBytes(query);
hwrq.ContentLength = data.Length;
hwrq.GetRequestStream().Write(data, 0, data.Length);
using (HttpWebResponse hwrs = (HttpWebResponse)hwrq.GetResponse())
{
using (StreamReader sr = new StreamReader(hwrs.GetResponseStream()))
{
return sr.ReadToEnd().Trim();
}
}
}
public HttpWebRequest CreateRequest(string url)
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
Request.UserAgent = UserAgent;
Request.Accept = Accept;
Request.Headers.Add("Accept-Language", AcceptLang);
Request.AutomaticDecompression = DMethod;
return Request;
}
I’ve been tracking the “query” variable, it stays with the “+” char, but when I see the request in sniffer (Charles), the request is sent without the “+”.
For example I’m trying to send
<…>zxJ+zZq<…>
and
<…>zxJ zZq<…>
is actually sent.
What am I doing wrong?
Thanks in advance.
+represents space in a query string. It looks like like yourqueryvariable that you are sending is not properly URL encoded. Unfortunately you haven’t shown how you are constructing thisquerystring variable but here’s the correct way in order to ensure that all values are properly URL encoded:Notice how the
+sign that was originally part of the value is encoded to%2B.If you write the following:
I think you see the problem for yourself and the difference between the correct query string which is
foo=some+%2b+value&bar=some+other+value.Now this being said here’s what I would suggest you in order to improve your code. You could extend the
WebClientclass so that it can handle cookies, like this:and now simply:
See how much easier this is instead of messing around with all those HttpWebRequests, responses, streams, encodings, etc…?