【C# HTTP】HttpWebRequest使用中编码问题
2012-07-30 18:34
603 查看
【问题】使用HttpWebRequest提交数据,总是返回错误。
string smstext = HttpUtility.UrlEncode(content); string objmobile = phoneno; string smsid = objmobile + DateTime.Now.ToString("yyyyMMddHHmmssFF"); string url = string.Format("http://www.XXXXX.cn/Xxx.php?loginname={0}&password={1}&tele={2}&msg={3}", usernname, _password, objmobile, smstext); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
【分析】猜测应该是编码问题。
【结论】
在上面的程序代码中,我们以 GET 方式访问了网址 传递了参数包含汉字,由于无法告知对方提交数据的编码类型,所以编码方式要以对方的网站为标准。常见的网站中, www.baidu.com (百度)的编码方式是 gb2312, www.google.com (谷歌)的编码方式是
utf8。
string smstext = HttpUtility.UrlEncode(content); string un = HttpUtility.UrlEncode(_username, Encoding.GetEncoding("GB2312")); string objmobile = phoneno; string smsid = objmobile + DateTime.Now.ToString("yyyyMMddHHmmssFF"); string url = string.Format("http://www.xxxx.cn/xxxx.php?loginname={0}&password={1}&tele={2}&msg={3}", un, _password, objmobile, smstext); log.Debug("GATEWAY" + GatewadID + " SENDURL:" + url); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
相关文章推荐
- c#使用HttpWebRequest上传文件同时携带其他参数
- 使用 HttpWebRequest 类调用 WEB 服务的示例(C#)
- 【原创】C#使用HttpWebRequest,HttpWebResponse
- C#在WinForm下使用HttpWebRequest上传文件并显示进度
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- C#中使用HttpWebRequest用Post提交MultiPart数据
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- c#中HttpWebRequest使用Proxy实现指定IP的域名请求
- C#中HttpWebRequest与HttpWebResponse的使用方法
- 在多线程环境下使用HttpWebRequest或者调用Web Service(连接报超时问题)
- 使用C#的HttpWebRequest模拟登陆网站
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- C# 使用HttpWebRequest用Post提交MultiPart数据
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- C#使用HttpWebRequest模拟登陆(附带验证码)
- [C#]在WinForm下使用HttpWebRequest上传文件并显示进度
- C#使用浏览器(Internet Explorer)默认的代理服务器设置(WebRequest,WebClient,HttpWebRequest)
- Xcode7 使用WebView loadRequest发送HTTP请求的问题
- C#在WinForm下使用HttpWebRequest上传文件并显示进度
- C#使用HttpWebRequest和HttpWebResponse上传文件示例