HttpWebRequest的Post问题

ruanyuping 2007-09-10 02:38:50
用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();


...全文
564 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyywt 2010-11-29
  • 打赏
  • 举报
回复
byte[] bytes = Encoding.GetEncoding( "gb2312 ").GetBytes(postData);
wubi 2007-09-23
  • 打赏
  • 举报
回复
我也遇到类似问题,我估计应该是类似于防盗链的机制在起作用,重新跳转后HttpWebRequest 无法伪造Referer信息,所以当服务器无法获取Referer的时候,判断为直接输入地址,而不是从本站跳转过来的。直接转向ERROR页面了!
至于怎么解决。。。我不知道。
ruanyuping 2007-09-18
  • 打赏
  • 举报
回复
谁知道是怎么回事
ruanyuping 2007-09-10
  • 打赏
  • 举报
回复
没什么好报答大家,只有用分数来报答了

现在分比较少,谁把问题解决了,等我凑足分,我再给100分

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧