C#逃生用加号(+)在POST HttpWebRequest的 [英] C# Escape Plus Sign (+) in POST using HttpWebRequest
问题描述
我有问题发送POST数据,其中包括像characteres+在密码字段,
<预类=郎-CS prettyprint-覆盖>
字符串POSTDATA =的String.Format(用户名= {0}&放大器;密码= {1},anyname,+ 13Gt2);
我使用HttpWebRequest和一个WebBrowser看到的结果,当我尝试的 HttpWebRequest的使用POST数据到该网站从我的C#的WinForms登录的,它告诉我,密码不正确。 (在源代码[richTexbox1]和webBrowser1)。与我的另一个帐户尝试它,不包含+字,它让我登录正确(使用的字节数组,并将其写入流)
<预类=郎-CS prettyprint-覆盖>
字节[]的字节数组= Encoding.ASCII.GetBytes(POSTDATA); //获取数据
request.Method =POST;
request.Accept =text / html的;
request.ContentType =应用/的X WWW的形式,进行了urlencoded
request.ContentLength = byteArray.Length;
方通流= request.GetRequestStream(); //打开连接
newStream.Write(字节数组,0,byteArray.Length); //发送数据。
newStream.Close(); //这个效果很好,如果用户不包括符号
此的Question 我发现 HttpUtility.UrlEncode()
是逃避非法字符的解决方案,但我不能找出如何正确使用它,我的问题是,在与进行urlencode()
如何将数据发送到我的要求的正确?
这是我怎么一直试图小时,以它的工作,但没有运气,搜索结果
第一种方法
<预类=郎-CS prettyprint-覆盖 >
串了urlencoded = HttpUtility.UrlEncode(POSTDATA,ASCIIEncoding.ASCII);
//request.ContentType =应用/的X WWW的形式,进行了urlencoded
request.ContentLength = urlEncoded.Length;
的StreamWriter WR =新的StreamWriter(request.GetRequestStream(),ASCIIEncoding.ASCII);
wr.Write(urlencoded的); //为密码错误,服务器返回。
wr.Close();
二法
<预类=郎-CS prettyprint-覆盖>
字节[] = urlEncodedArray HttpUtility.UrlEncodeToBytes(POSTDATA,ASCIIEncoding.ASCII);
方通流= request.GetRequestStream(); //打开连接
newStream.Write(urlEncodedArray,0,urlEncodedArray.Length); //发送数据。
newStream.Close(); //服务器告诉我同样的事情..
我觉得我做错了就如何URL编码必须发送的请求,我真的问一些帮助,请,我通过谷歌搜索,但没有找到有关如何编码的URL发送给更多信息的HttpWebRequest的..我很欣赏你的时间和精力,希望你能帮我。谢谢你。
我发现我的答案使用 Uri.EscapeDataString
它正好解决我的问题,但不知何故,我不能 HttpUtility.UrlEncode
做到这一点。的 Stackoverflowing各地的,我发现这个问题即大约进行urlencode方法, MSDN 该文件告诉:
编码为URL字符串。
块引用>
但我知道现在如果用于编码POST数据是错误(@Marvin,@Polity,感谢校正)。丢包后,我试过如下:
<预类=郎-CS prettyprint-覆盖>字符串POSTDATA =的String.Format(用户名= {0}&放大器;密码= {1},anyname,Uri.EscapeDataString(+ 13Gt2));
POST数据被转换成:
<预类=郎-CS prettyprint-覆盖>// **输出
串的用户名= anyname;
字符串密码=%2B13Gt2;
Uri.EscapeDataString
中的 MSDN 说以下内容:
一个字符串,它逃过表示形式。
块引用>
我想,这就是我一直在寻找,当我试图在上面,我可以张贴正确只要有数据,包括在FORMDATA的+字,但不知有多少了解他们。
这环节是真的很有帮助。
非常感谢您的答案和你的时间,我非常感激。问候队友。
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 withurlEncode()
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 methodstring 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.
解决方案I found my answer using
Uri.EscapeDataString
it exactly solved my problem, but somehow I couldn't do it withHttpUtility.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.
Thanks a lot for the answers and your time, I appreciate it very much. Regards mates.
这篇关于C#逃生用加号(+)在POST HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!