积极答复者
急!搞了我一夜还是不行!GetResponseStream()返回乱码!怎么修改encoding都不成!求老师们帮帮忙!

问题
-
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; namespace alvvtest { class Program { static void Main(string[] args) { string postzf = "pageindex=1"; string url = "http://www.alivv.com/ajax/Alivv.AjaxRun.WebSiteAjax,Alivv.AjaxRun.ashx?_method=BuyerWebSiteList&_session=r"; HttpWebRequest newqq = null; HttpWebResponse newfh = null; System.Net.ServicePointManager.Expect100Continue = false; Uri newurl = new Uri(url); newqq = (HttpWebRequest)WebRequest.Create(newurl); string data = postzf; newqq.Host = "www.alivv.com"; newqq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6; .NET CLR 2.0.50727; CIBA)"; newqq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; newqq.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); newqq.Headers.Add("Accept-Encoding", "gzip, deflate"); newqq.Headers.Add("DNT", "1"); newqq.ContentType = "text/plain; charset=UTF-8"; newqq.Referer = "http://www.alivv.com/links_gd.html"; newqq.CookieContainer = new CookieContainer() ; newqq.Accept = "*/*"; newqq.Method = "POST"; newqq.Timeout = 60000; //newqq.KeepAlive = true; //newqqt.AllowAutoRedirect = true; byte[] byteArray = Encoding.UTF8.GetBytes(data); Stream stream = newqq.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); newfh = (HttpWebResponse)newqq.GetResponse(); StreamReader sr = new StreamReader(newfh.GetResponseStream(), Encoding.UTF8); string fanhuizhi = sr.ReadToEnd(); } } }
最后的fanhuizhi是乱码不管怎么修改encoding都不行
目前我发现在头协议当中有一句:
newqq.Headers.Add("Accept-Encoding", "gzip, deflate");
难道说信息流通过压缩了?
还有就是post部分的代码是否正确呢?希望老师们帮我看一下,被这个问题搞死了!
附上正常返回的值:
正常返回的值是通过浏览器的抓包软件抓到的,谢谢大家!!
答案
-
拿到的可能是一个 Gzip 过的 Response,因为指定了 accept=gzip.
试试用 System.IO.Compression.GZipStream 试试看吧,默认浏览器会给你自动解压缩的。所以看得到具体的 Response。
另外,请检查一下 GetResponse 之后的 Response Headers,里面应该有说这个 Response 是不是压缩过的。(Content-Encoding)
Mark Zhou
- 已标记为答案 Jason Dot WangModerator 2012年9月3日 8:45
-
newqq.Headers.Add("Accept-Encoding", "gzip, deflate");
你指定gzip或deflate了呀,服务器返回的当然就是GZip流或者是Deflate流了。
服务器具体使用的是哪种可以通过Response Header中的Encoding来得知。然后使用GZipStream或者DeflateStream进行解压缩就可以了,这两种压缩流.NET都已经内置支持
共同努力,共同提高
kaedei#live.cn My BLOG- 已标记为答案 Jason Dot WangModerator 2012年9月3日 8:45
全部回复
-
拿到的可能是一个 Gzip 过的 Response,因为指定了 accept=gzip.
试试用 System.IO.Compression.GZipStream 试试看吧,默认浏览器会给你自动解压缩的。所以看得到具体的 Response。
另外,请检查一下 GetResponse 之后的 Response Headers,里面应该有说这个 Response 是不是压缩过的。(Content-Encoding)
Mark Zhou
- 已标记为答案 Jason Dot WangModerator 2012年9月3日 8:45
-
newqq.Headers.Add("Accept-Encoding", "gzip, deflate");
你指定gzip或deflate了呀,服务器返回的当然就是GZip流或者是Deflate流了。
服务器具体使用的是哪种可以通过Response Header中的Encoding来得知。然后使用GZipStream或者DeflateStream进行解压缩就可以了,这两种压缩流.NET都已经内置支持
共同努力,共同提高
kaedei#live.cn My BLOG- 已标记为答案 Jason Dot WangModerator 2012年9月3日 8:45
-
newqq.Headers.Add("Accept-Encoding", "gzip, deflate");直接去掉这句就可以了,不过你post的数据不完整,得到的结果是服务器端抛出的错误,要得到正确数据将postzf 写完整即可
- 已编辑 yiyanxiyin 2012年9月5日 1:27