chunked编码的内容如何解码

sunny906 2011-04-24 11:32:50

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();
...全文
518 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghao2979 2012-09-22
  • 打赏
  • 举报
回复
我也遇到这问题。

不过看到 size /r/n content /r/n 之类的,还在研究中
sunny906 2011-04-24
  • 打赏
  • 举报
回复
谢谢yanyangfei
是chunked编码问题,不知道如何解码
yanyangfei 2011-04-24
  • 打赏
  • 举报
回复
是不是用了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();
}
sunny906 2011-04-24
  • 打赏
  • 举报
回复
期待中...
sunny906 2011-04-24
  • 打赏
  • 举报
回复
chunked,数据是按块传输的,也就是数据是由若干个chunk组成的
问题是怎么获取每个chunk,只要能获取每一个块的大小和内容,问题就可以解决了
sunny906 2011-04-24
  • 打赏
  • 举报
回复
谢谢sxldfang
现在没有乱码了,不过都是繁体中文
sxldfang 2011-04-24
  • 打赏
  • 举报
回复
你得到是有乱码有可能你用Encoding。Default去解码,实际网页采用的是 utf8 编码
sxldfang 2011-04-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sunny906 的回复:]
请问:getBytes(string url,CookieContainer cookie) ,这个方法中的CookieContainer cookie如何获取它的值?
[/Quote]
若页面需要登录才能获取网页,那就需要CookieContainer,不过对于你给出的网址,用null参数就可以啦!
sunny906 2011-04-24
  • 打赏
  • 举报
回复
有了解chunked解码的吗?
sunny906 2011-04-24
  • 打赏
  • 举报
回复
请问:getBytes(string url,CookieContainer cookie) ,这个方法中的CookieContainer cookie如何获取它的值?
sxldfang 2011-04-24
  • 打赏
  • 举报
回复
这是我访问到的页面内容:

前面的部分

<!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&hl=zh-TW&newwindow=1&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 编码

110,534

社区成员

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

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

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