c#转义加号(+)在POST使用HttpWebRequest

本文关键字:POST 使用 HttpWebRequest 转义 | 更新日期: 2023-09-27 18:17:51

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

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

我使用HttpWebRequest和一个网络浏览器来查看结果,当我尝试登录从我的c# WinForms使用HttpWebRequest 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编码后,我如何将数据正确发送到我的请求 ?

我花了好几个小时想让它工作,但是运气不好,

第一个方法

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的信息。感谢您的时间和关注,希望您能帮助我。谢谢你。

c#转义加号(+)在POST使用HttpWebRequest

我用Uri.EscapeDataString找到了我的答案,它完全解决了我的问题,但不知何故我不能用HttpUtility.UrlEncode做到这一点。 stackoverflow围绕,我发现这个问题是关于urlEncode方法,在msdn it文档中告诉:

编码URL字符串

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

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

POST数据被转换成:

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

Uri.EscapeDataString在msdn中表示以下内容:

将字符串转换为转义后的表示形式。

我想这就是我想要的,当我尝试上面的,我可以正确地张贴每当有数据包括'+'字符在formdata,但不知何故有很多要了解他们。

这个链接真的很有用。

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

非常感谢你的回答和你的时间,我非常感激。视伴侣。

我建议你使用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);
}

您发送的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。

希望有帮助,