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

http://www.cnblogs.com/stone_w/archive/2011/01/18/1938386.html

 

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());
}

posted @ 2011-10-22 11:21  云中雀  阅读(5468)  评论(0编辑  收藏  举报