天天看點

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,如需轉載請自行聯系原作者

繼續閱讀