//...准备HTTP头,一切就绪...
                socket.Send(ms);
                int size = 1024, recv = 0;
                byte[] data = new byte[size];
                StringBuilder retHtml = new StringBuilder();
                Encoding realHtmlEcnodType = htmlEncodType == null ? Encoding.Default : htmlEncodType;
                while (true)
                {
                    recv = socket.Receive(data, data.Length, SocketFlags.None);
                    if (recv <= 0) break;
                    retHtml.Append(realHtmlEcnodType.GetString(data, 0, recv));
                }
                //关闭链接
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
                return retHtml.ToString();
当http发送头含Accept-Encoding: gzip,deflate时,
接收回来的数据,找不到正文了,如这里以获取百度HTML为例...
含gzip,deflate时返回如下代码:HTTP/1.1 200 OK
Date: Sun, 22 May 2011 09:19:13 GMT
Server: BWS/1.0
Content-Length: 3302
Content-Type: text/html;charset=gb2312
Cache-Control: private
Expires: Sun, 22 May 2011 09:19:13 GMT
Content-Encoding: gzip
Set-Cookie: BAIDUID=307B19BD9F9A8295A74F7183E28767F0:FG=1; expires=Sun, 22-May-41 09:19:13 GMT; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Connection: Close?
当http发送头不含Accept-Encoding: gzip,deflate时,能正确返回所有代码(包含正文),如下:HTTP/1.1 200 OK
Date: Sun, 22 May 2011 09:20:44 GMT
Server: BWS/1.0
Content-Length: 7151
Content-Type: text/html;charset=gb2312
Cache-Control: private
Expires: Sun, 22 May 2011 09:20:44 GMT
Set-Cookie: BAIDUID=9765DC6793535F67D42F1C99A1FF6E58:FG=1; expires=Sun, 22-May-41 09:20:44 GMT; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Connection: Close<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>百度一下,你就知道      </title><style>body{font:12px arial;text-align:center;background:#fff}body,p,form,ul{margin:0;padding:0}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{padding:7px 10px 3px 0;text-align:right}#m{width:680px;margin:0 auto}#nv{font-size:16px;margin:0 0 4px;text-align:left;text-indent:117px}#nv a,#nv b,.btn,#lk{font-size:14px}#fm{padding-left:90px;text-align:left}#kw{width:404px;height:22px;padding:4px 7px;padding:6px 7px 2px\9;font:16px arial;background:url(http://www.baidu.com/img/i2.png) no-repeat;border:1px solid #cdcdcd;border-color:#9a9a9a #cdcdcd #cdcdcd #9a9a9a;vertical-align:top}.btn{width:95px;height:32px;padding:0;padding-top:2px\9;border:0;background:#ddd url(http://www.baidu.com/img/i2.png) 0 -35px;cursor:pointer}.btn_h{background-position:-100px -35px}#kw,.btn_wr{margin:0 5px 0 0}.btn_wr{width:97px;height:34px;display:inline-block;background:url(http://www.baidu.com/img/i2.png) no-repeat -202px bottom;_padding-top:1px;*position:relative}#lk{margin:33px 0}#lk span{font:14px "宋体"}#lm{height:60px}#lh{margin:16px 0 5px;word-spacing:3px}#mCon{position:absolute;right:7px;top:10px;cursor:pointer;padding:0 18px 0 0;background:url(http://www.baidu.com/img/arr.gif) no-repeat right 0}#mCon span{color:#00c;cursor:default;display:block;*padding-top:2px}#mCon .hw{text-decoration:underline;cursor:pointer}#mMenu{width:56px;border:1px solid #9a99ff;list-style:none;position:absolute;right:7px;top:28px;display:none;background:#fff}#mMenu a{width:100%;height:100%;display:block;line-height:22px;text-indent:6px;text-decoration:none}#mMenu a:hover{background:#d9e1f6}#mMenu .ln{height:1px;background:#ccf;overflow:hidden;margin:2px;font-size:1px;line-height:1px}#cp,#cp a{color:#77c}</style></head>
<body><p id="u"><a href="/gaoji/preferences.html">搜索设置</a>&nbsp;|&nbsp;<a href="http://passport.baidu.com/?login&tpl=mn">登录</a></p><div id="m"><p id="lg"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" width="270" height="129" usemap="#mp"></p><p id="nv"><a href="http://news.baidu.com">新&nbsp;闻</a> <b>网&nbsp;页</b> <a href="http://tieba.baidu.com">贴&nbsp;吧</a> <a href="http://zhidao.baidu.com">知&nbsp;道</a> <a href="http://mp3.baidu.com">MP3</a> <a href="http://image.baidu.com">图&nbsp;片</a> <a href="http://video.baidu.com">视&nbsp;频</a> <a href="http://map.baidu.com">地&nbsp;图</a></p><div id="fm"><form name="f" action="/s"><input type="text" name="wd" id="kw" maxlength="100"><span class="btn_wr"><input type="submit" value="百度一下" id="su" class="btn" onmousedown="this.className='btn btn_h'" onmouseout="this.className='btn'"></span></form><div id="mCon"><span>输入法</span></div><ul id="mMenu"><li><a href="#" name="ime_hw">手写</a></li><li><a href="#" name="ime_py">拼音</a></li><li class="ln"></li><li><a href="#" name="ime_cl">关闭</a></li></ul></div>
<p id="lk"><a href="http://hi.baidu.com">空间</a> <a href="http://baike.baidu.com">百科</a> <a href="http://www.hao123.com">hao123</a><span> | <a href="/more/">更多&gt;&gt;</a></span></p><p id="lm"></p><p><a id="sh" onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com" onmousedown="return ns_c({'fm':'behs','tab':'homepage','pos':0})">把百度设为主页</a></p><p id="lh"><a href="http://e.baidu.com/?refer=888">加入百度推广</a> | <a href="http://top.baidu.com">搜索风云榜</a> | <a href="http://home.baidu.com">关于百度</a> | <a href="http://ir.baidu.com">About Baidu</a></p><p id="cp">&copy;2011 Baidu <a href="/duty/">使用百度前必读</a> <a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a> <img src="http://gimg.baidu.com/img/gs.gif"></p></div><map name="mp"><area shape="rect" coords="40,25,230,95" href="http://hi.baidu.com/baidu/" target="_blank" title="点此进入 百度的空间"></map></body>
<script>var w=window,d=document,n=navigator,k=d.f.wd,a=d.getElementById("nv").getElementsByTagName("a"),isIE=n.userAgent.indexOf("MSIE")!=-1;if(!isIE||window.opera){d.getElementById("sh").style.display="none"}for(var i=0;i<a.length;i++){a[i].onclick=function(){if(k.value.length>0){var C=this,A=C.href,B=encodeURIComponent(k.value);if(A.indexOf("q=")!=-1){C.href=A.replace(/q=[^&$]*/,"q="+B)}else{this.href+="?q="+B}}}}(function(){if(/q=([^&]+)/.test(location.search)){k.value=decodeURIComponent(RegExp.$1)}})();if(n.cookieEnabled&&!/sug?=0/.test(d.cookie)){d.write('<script src=http://www.baidu.com/js/bdsug.js?v=1.0.3.0><\/script>')}function addEV(C,B,A){if(w.attachEvent){C.attachEvent("on"+B,A)}else{if(w.addEventListener){C.addEventListener(B,A,false)}}}function G(A){return d.getElementById(A)}function ns_c(E){var F=encodeURIComponent(window.document.location.href),D="",A="",B="",C=window["BD_PS_C"+(new Date()).getTime()]=new Image();for(v in E){A=E[v];D+=v+"="+A+"&"}B="&mu="+F;C.src="http://nsclick.baidu.com/v.gif?pid=201&pj=www&"+D+"path="+F+"&t="+new Date().getTime();return true}var bdimeHW={};var imeTar="kw";var ime_t1=(new Date()).getTime();(function(){var O=G("mCon"),A=G("mMenu");var B=["输入法","手写","拼音"],Q=["cl","hw","py"],D=["","http://www.baidu.com/hw/hwInput_1.1.js","http://www.baidu.com/olime/bdime.js"],P=[0,0,0];var N=n.cookieEnabled;if(N&&/\bbdime=(\d)/.test(d.cookie)){H(Q[RegExp.$1],false)}var M=A.getElementsByTagName("a");for(var I=0;I<M.length;I++){M[I].onclick=F}if(isIE){var E=[];var R=O.getElementsByTagName("*");for(var I=0;I<R.length;I++){E.push(R[I])}E.push(O);var R=A.getElementsByTagName("*");for(var I=0;I<R.length;I++){E.push(R[I])}E.push(A);for(var I=0;I<E.length;I++){E[I].setAttribute("unselectable","on")}}else{try{var L=k.value.length;k.selectionStart=L;k.selectionEnd=L;bdimeHW.hasF=1}catch(K){}}function F(){ime_t1=(new Date()).getTime();var T=this.name.split("_")[1];try{if(w.bdime){bdime.control.closeIme()}}catch(S){}H(T,true);return false}function H(X,S){var V=0;if(X==Q[1]){V=1;O.innerHTML='<span id="imeS" class="hw">'+B[1]+"</span>";if(isIE){G("imeS").setAttribute("unselectable","on")}function U(){if(!P[1]){if(d.selection&&d.activeElement.id&&d.activeElement.id=="kw"){bdimeHW.hasF=1}bdimeHW.input=imeTar;bdimeHW.submit="su";J(D[1]);setTimeout(function(){if(bdsug){bdsug.sug.initial()}},1000);P[1]=1}else{bdimeHW.reload(S)}}if(S){U()}else{addEV(G("imeS"),"click",U)}}else{if(X==Q[2]){V=2;O.innerHTML="<span>"+B[2]+"</span>";if(!P[2]){J(D[2]);P[2]=1}else{try{if(w.bdime){bdime.control.openIme()}}catch(W){}}}else{O.innerHTML="<span>"+B[0]+"</span>"}}if(S&&N){var T=new Date();T.setTime(T.getTime()+365*24*3600*1000);d.cookie="bdime="+V+";domain=baidu.com;path=/;expires="+T.toGMTString()}}function J(S){if(S){var T=d.createElement("script");T.src=S;d.getElementsByTagName("head")[0].appendChild(T)}}function C(T){var T=T||window.event;var S=T.target||T.srcElement;A.style.display=S.id=="mCon"&&A.style.display!="block"?"block":"none"}addEV(d,"click",C)})();addEV(w,"load",function(){k.focus()});w.onunload=function(){};</script></html><!--ee619c3270598803-->因为工作需要,得写一个C# socket 的httpclient(不能用HttpWebRequest及其它相关的类,只能自己写httpclient),同时也需要用到gzip,deflate(这样体积小,速度快),但加了gzip,deflate,就无法返回正文,怎么解决呢?

解决方案 »

  1.   

    gzip的时候,收下来是压缩的流,你得解压啊。
      

  2.   

    这个代码就很成问题。首先,客户端的Encoding.Default跟服务器输出的Encoding没有关系。其次,socket.Receive得到的字节流应该综合在一起才能转换为字符串,不能想当然地按照1024个字节分隔。最后,#1楼已经说了,有时候收到的是按字节压缩的数据,不是什么字符串。
      

  3.   

    关键是 retHtml.Append(realHtmlEcnodType.GetString(data, 0, recv)); 不对。你不能把 gzip 的流直接转成 string
    把流都放到byte[](下面的data即byte[])后,再通过下面的代码转换
    result 是 string 的结果。using(MemoryStream ms = new MemoryStream(data))
    {
        using(GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress))
            using (StreamReader sr = new StreamReader(zipStream, encoding))
                result = sr.ReadToEnd();
    }