使用HttpWebRequest在POST中使用C#Escape Plus Sign(+)

我在发送POST数据时遇到问题,包括密码字段中的“+”字符,

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

我正在使用HttpWebRequest和webbrowser来查看结果,当我尝试使用HttpWebRequest从我的C#WinForms 登录到POST数据到网站时,它告诉我密码不正确。 (在源代码[richTexbox1]和webBrowser1中)。 尝试使用我的另一个帐户,它不包含’+’字符,它允许我正确登录(使用字节数组并将其写入流)

 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 

从这个问题我发现HttpUtility.UrlEncode()是逃避非法字符的解决方案,但我无法找到如何正确使用它,我的问题是,在使用urlEncode()对我的POST数据进行url编码之后我该怎么办?正确地将数据发送给我的请求

这就是我一直在尝试HOURS让它发挥作用,但没有运气,

第一种方法

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

第二种方法

 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.. 

我认为我在如何将url编码必须发送到请求上做错了,我真的请求一些帮助,我通过谷歌搜索,无法找到有关如何将编码的URL发送到HttpWebRequest的更多信息。我感谢您的时间和关注,希望您能帮助我。 谢谢。

我推荐你一个WebClient 。 将缩短您的代码并处理编码和内容:

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

我使用Uri.EscapeDataString找到了我的答案它完全解决了我的问题,但不知何故我用HttpUtility.UrlEncodeStackoverflowing ,我发现这个问题是关于urlEncode方法,在msdn它的文档告诉:

对URL字符串进行编码。

但我现在知道如果用于编码POST数据是错误的(@Marvin,@ Polity,感谢更正)。 丢弃后,我尝试了以下内容:

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

POST数据转换为:

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

msdn中的 Uri.EscapeDataString表示如下:

将字符串转换为其转义表示forms。

我认为这就是我所寻找的,当我尝试上述内容时,只要有数据包括formdata中的’+’字符,我就可以正确发布,但不知何故,有很多东西要学习它们。

这个链接真的很有帮助。

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

非常感谢你的答案和时间,我非常感激。 伙伴们。

你发送的postdata不应该是URL编码的! 它是formdata,而不是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()); 

这使用System.Text.Encoding.ASCII对postdata进行编码。

希望这可以帮助,