string url="http://www.google.com.hk/search?q=c";
            string html="";
            WebClient web = new WebClient();
            web.Headers.Add(HttpRequestHeader.Referer, url);
            html = web.DownloadString(url);  //得到的响应数据里有乱码,原因是Transfer-Encoding:chunked,该如何解码?
            web.Dispose();

解决方案 »

  1.   

    是不是用了gzip试下这个 
    WebClient web = new WebClient();
            Encoding encoding = Encoding.GetEncoding("utf-8");
            string result = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 30000;
            //设置连接超时时间
            request.Method = "GET";
            request.UserAgent = "Googlebot/2.1 (+http://www.google.com/bot.html)";
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (Stream streamReceive = response.GetResponseStream())
            {
                using (GZipStream zipStream = new GZipStream(streamReceive, CompressionMode.Decompress))
                using (StreamReader sr = new StreamReader(zipStream, encoding))
                    result = sr.ReadToEnd();
            }
      

  2.   

    谢谢yanyangfei
    是chunked编码问题,不知道如何解码
      

  3.   

    这是我访问到的页面内容:前面的部分:<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>c - Google 搜尋</title><style>#gog{background:#fff}#gbar,#guser{font-size:13px;padding-top:1px !important}#gbar{float:left;height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh
    后面的部分:type="text" name="q" maxlength="2048" value="c" title="搜尋"></div></td><td><div class="ds"><div class="lsbb"><input type="submit" name="btnG" class="lsb" value="搜尋"></div></div></td></tr></table><input type="hidden" name="hl" value="zh-TW"></form></div><p id="bfl" class="flc" style="margin:6px 0 0;text-align:center"><a href="/intl/zh-TW/help.html">搜尋說明</a> <a href="/quality_form?q=c&amp;hl=zh-TW&amp;newwindow=1&amp;prmd=ivnsb">請提供您寶貴的意見</a></p></div><div id="fll" class="flc" style="margin:19px auto 19px auto;text-align:center"><a href="/"> Google 首頁 </a> <a href="
             /intl/zh-TW/ads/">廣告計劃</a> <a href="/intl/zh-TW/privacy.html">隱私權政策</a> <a href="/intl/zh-TW/about.html">Google 完全手冊</a></div></div></td><td valign="top"></td></tr></table><script src="/extern_js/f/CgV6aC1UVxICaGsrMFo4ACwrMA44ACwrMAo4AEACLCswGDgALIACQpACOQ/9wB50KX8WSo.js"></script><script type="text/javascript">
          var form=document.gs;
          google.ac.i(form,form.q,'','c','',{l:1,sw:1,d:1,he:'sbhost',vc:-3});</script></body></html>读取的方法参考读取网络资源,返回字节数组网页采用的是 utf8 编码
      

  4.   

    请问:getBytes(string url,CookieContainer cookie) ,这个方法中的CookieContainer cookie如何获取它的值?
      

  5.   

    若页面需要登录才能获取网页,那就需要CookieContainer,不过对于你给出的网址,用null参数就可以啦!
      

  6.   

    你得到是有乱码有可能你用Encoding。Default去解码,实际网页采用的是 utf8 编码
      

  7.   

    谢谢sxldfang
    现在没有乱码了,不过都是繁体中文
      

  8.   

    chunked,数据是按块传输的,也就是数据是由若干个chunk组成的
    问题是怎么获取每个chunk,只要能获取每一个块的大小和内容,问题就可以解决了
      

  9.   

    我也遇到这问题。不过看到  size   /r/n   content /r/n 之类的,还在研究中