HttpWebRequest的Post问题

ruanyuping ruanyuping 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
用HttpWebRequest向一个表单POST, POST是成功了,得到的结果没有乱码,但是URL被表单重定向到一个含有中文乱码的地址,所以得到的结果也是错的,我知道结果可以设置编码来读取就不会乱码了,但是重定向地址要怎么设置才不会乱码?

重定向后的地址: http://www.xxxx.com/showerr.asp?ErrCodes=<li>ÇëÊäÈëÄúµÄÓû§Ãû(³¤¶È²»ÄÜ´óÓÚ20»òСÓÚ1)¡£&action=OtherErr

代码:
byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(postData);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Accept-Language", "zh-cn");
request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
request.ContentType = "application/x-www-form-urlencoded; charset=bg2312";
request.Method = "POST";
request.ContentLength = bytes.Length;

Stream os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null) return null;

StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));

string responseUrl = response.ResponseUri.ToString();
string result = sr.ReadToEnd().Trim();


...全文
评论 4
收藏 已收藏
分享
返回顶部
ruanyuping
C# 111,129 成员 642,539 内容