HttpWebRequest模拟POST提交防止中文乱码
测试通过,请求的为自己写的一般处理程序,代码如下:
代码
Encoding myEncoding = Encoding.GetEncoding("gb2312");
string param = HttpUtility.UrlEncode("aa", myEncoding) + "=" +
HttpUtility.UrlEncode("值A", myEncoding);
byte[] bs = Encoding.UTF8.GetBytes(param);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://xxxx.com");
req.Method = "POST";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = bs.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
WebResponse myWebResponse = req.GetResponse();
using (StreamReader sr = new StreamReader(myWebResponse.GetResponseStream(), myEncoding))
{
// 返回结果
Response.Write(sr.ReadToEnd());
}
关注下面二维码,订阅更多精彩内容。