无法在http发布请求C#中发送特殊字符
我正在使登录过程自动化,并且在这样做的过程中,我将获得不同类型的密码,其中包括特殊字符.
I am automating the process of sign in and in the process of doing so, I will get different type of passwords which will include special characters.
我的代码如下,并且我尝试使用UrlEncode()密码,该密码不起作用.如果您在我的代码中发现任何问题,或者我可以通过哪种方式找到答案,请告诉我.我的密码是"aab $#*#%232"和"@#:.; $%^& + -__ h1&":
My code is as below and I have tried to UrlEncode() the password, which didn't work. Please let me know if you find any issues in my code or which way i can find a work out. My passwords are "aab$#*#%232" and "@#:.;$%^&+-_h1&" :
string uriString = "http://" + IP + URI ;
string postData = "";
TraceLine("The uri string is " + uriString);
foreach (string key in values.AllKeys)
{
TraceLine(key + " " + values[key]);
postData += key + "=" + values[key] + "&";}}
if (postData.Length > 0) {
postData = postData.TrimEnd(postData[postData.Length - 1]);
}
TraceLine("The postData string is " + postData);
HttpWebRequest req =(HttpWebRequest)System.Net.WebRequest.Create(uriString);
req.ContentType = "application/x-www-form-urlencoded";
req.KeepAlive = false;
req.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
req.ContentLength=bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();}
您实际上得到了正确的结果.请参阅,对该字符串进行了转义以使其兼容通过HTTP发送.您想拥有:"aab $##%232",但您却拥有:"aab%24%23 %23%25232"
%24 = $
%23 =#
%25 =%
You're actually having the correct results. See, the string is escaped to make it compatible for sending over HTTP. You wanted to have: "aab$##%232" but you got: "aab%24%23%23%25232"
%24 = $
%23 = #
%25 = %
In order to have the string you want back you just have to Un-Escape the string using the Uri.UnescapeDataString method.
string str = Uri.UnescapeDataString("aab%24%23*%23%25232");
尽管如此,我还是想劝阻您不要以纯文本格式发送和接收敏感数据.也许加密有帮助吗?
Still I'd like to discourage you from sending and receiving sensitive data in plain text, even if its escaped. Maybe something from encryption could help?