C# Escape Plus Sign (+) in POST using HttpWebReque

2019-03-05 06:56发布

问题:

I'm having issues to send POST data that includes characteres like "+" in the password field,

string postData = String.Format("username={0}&password={1}", "anyname", "+13Gt2");

I'm using HttpWebRequest and a webbrowser to see the results, and when I try to log in from my C# WinForms using HttpWebRequest to POST data to the website, it tells me that password is incorrect. (in the source code[richTexbox1] and the webBrowser1). Trying it with another account of mine, that does not contain '+' character, it lets me log in correctly (using array of bytes and writing it to the stream)

byte[] byteArray = Encoding.ASCII.GetBytes(postData); //get the data
request.Method = "POST"; 
request.Accept = "text/html";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream newStream = request.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close(); //this works well if user does not includes symbols

From this Question I found that HttpUtility.UrlEncode() is the solution to escape illegal characters, but I can't find out how to use it correctly, my question is, after url-encoding my POST data with urlEncode() how do I send the data to my request correctly?

This is how I've been trying for HOURS to make it work, but no luck,

First method

string urlEncoded = HttpUtility.UrlEncode(postData, ASCIIEncoding.ASCII);
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = urlEncoded.Length;
StreamWriter wr = new StreamWriter(request.GetRequestStream(),ASCIIEncoding.ASCII);
wr.Write(urlEncoded); //server returns for wrong password.
wr.Close();

Second method

byte[] urlEncodedArray = HttpUtility.UrlEncodeToBytes(postData,ASCIIEncoding.ASCII);
Stream newStream = request.GetRequestStream(); //open connection
newStream.Write(urlEncodedArray, 0, urlEncodedArray.Length); // Send the data.
newStream.Close();  //The server tells me the same thing..

I think I'm doing wrong on how the url-encoded must be sent to the request, I really ask for some help please, I searched through google and couldn't find more info about how to send encoded url to an HttpWebRequest.. I appreciate your time and attention, hope you can help me. Thank you.

回答1:

I'd recommend you a WebClient. Will shorten your code and take care of encoding and stuff:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    { 
        { "username", "anyname" },
        { "password", "+13Gt2" },
    };
    var url = "http://foo.com";
    var result = client.UploadValues(url, values);
}


回答2:

I found my answer using Uri.EscapeDataString it exactly solved my problem, but somehow I couldn't do it with HttpUtility.UrlEncode. Stackoverflowing around, I found this question that is about urlEncode method, in msdn it documentation tells that:

Encodes a URL string.

But I know now that is wrong if used to encode POST data (@Marvin, @Polity, thanks for the correction). After discarding it, I tried the following:

string postData = String.Format("username={0}&password={1}", "anyname", Uri.EscapeDataString("+13Gt2"));

The POST data is converted into:

// **Output
string username = anyname;
string password = %2B13Gt2;

Uri.EscapeDataString in msdn says the following:

Converts a string to its escaped representation.

I think this what I was looking for, when I tried the above, I could POST correctly whenever there's data including the '+' characters in the formdata, but somehow there's much to learn about them.

This link is really helpful.

http://blogs.msdn.com/b/yangxind/archive/2006/11/09/don-t-use-net-system-uri-unescapedatastring-in-url-decoding.aspx

Thanks a lot for the answers and your time, I appreciate it very much. Regards mates.



回答3:

the postdata you are sending should NOT be URL encoded! it's formdata, not the URL

  string url = @"http://localhost/WebApp/AddService.asmx/Add";
  string postData = "x=6&y=8";

  WebRequest req = WebRequest.Create(url);
  HttpWebRequest httpReq = (HttpWebRequest)req;

  httpReq.Method = WebRequestMethods.Http.Post;
  httpReq.ContentType = "application/x-www-form-urlencoded";
  Stream s = httpReq.GetRequestStream();
  StreamWriter sw = new StreamWriter(s,Encoding.ASCII);
  sw.Write(postData);
  sw.Close();

  HttpWebResponse httpResp = 
                 (HttpWebResponse)httpReq.GetResponse();
  s = httpResp.GetResponseStream();
  StreamReader sr = new StreamReader(s, Encoding.ASCII);
  Console.WriteLine(sr.ReadToEnd());

This uses the System.Text.Encoding.ASCII to encode the postdata.

Hope this helps,