【C# HTTP】HttpWebRequest使用中编码问题

来源:互联网 发布:mysql order by 原理 编辑:程序博客网 时间:2024/04/30 22:28
【问题】使用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);


原创粉丝点击