日期:2014-05-18  浏览次数:20871 次

帮看看 HttpWebRequest 相关的一段代码
C# code

        static public int GetHtml(HttpWebRequest request, ref string strContent)
        {
            try
            {
                strContent = "";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();

                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    responseStream = new GZipStream(responseStream, CompressionMode.Decompress, true);
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
                }

                string strCharacterSet = response.CharacterSet;

                if (strCharacterSet.Contains("ISO-8859-1"))
                {
                    strCharacterSet = "gbk";
                }


                StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding(strCharacterSet));
                strContent = readStream.ReadToEnd();

                responseStream.Close();

                response.Close();


                return 0;
            }
            catch(Exception ex)
            {
                CMyFun.WriteLog(ex.ToString());
                return -1;
            }

        }




偶尔出错, 什么原因?
-----------------------------------------------------
2011-08-11 18:28:15 System.ArgumentException: 基流不可读。
参数名: stream
  在 System.IO.Compression.DeflateStream..ctor(Stream stream, CompressionMode mode, Boolean leaveOpen, Boolean usingGZip)
  在 System.IO.Compression.GZipStream..ctor(Stream stream, CompressionMode mode, Boolean leaveOpen)
  在 Robot.CMyFun.GetHtml(HttpWebRequest request, String& strContent) 位置 D:\workspace\WOWRobot_20110218_multi\CMyFun.cs:行号 63




------解决方案--------------------
或许是服务器端压缩时有一些bug吧。服务器端跟你客户端使用了同一个压缩/解压缩工具吗?