天天看点

HttpWebRequest模拟POST提交防止中文乱码

测试通过,请求的为自己写的一般处理程序,代码如下:

HttpWebRequest模拟POST提交防止中文乱码
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());

}

HttpWebRequest模拟POST提交防止中文乱码

本文转自王磊的博客博客园博客,原文链接:http://www.cnblogs.com/vipstone/archive/2011/01/18/1938386.html,如需转载请自行联系原作者

继续阅读