您的位置:首页 > 理论基础 > 计算机网络

【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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐