无法在http发布请求C#中发送特殊字符 [英] Unable to send special characters in http post request , C#

查看:121
本文介绍了无法在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?

这篇关于无法在http发布请求C#中发送特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆