我通过winform程序向某一网站提交表单的时候,是通过stream形式返回网站的信息,但在对stream进行读操纵的时候发现总是要用15秒钟才能读完,如果说是跟网站速度有关的话我通过IE同样提交一个表单网页监控工具发现只要来1秒,为什么差距这么大呢。而别人用delphi开发就能做到这个时间。往高手指教,是net的问题吗?现在把代码贴给大家....
  /// <summary>
        /// 获取指定页面的HTML代码
        /// </summary>
        /// <param name="url">指定页面的路径</param>
        /// <param name="postData">回发的数据</param>
        /// <param name="isPost">是否以post方式发送请求</param>
        /// <param name="cookieCollection">Cookie集合</param>
        /// <returns></returns>
        public string GetHtmlLhb(string url, string postData, bool isPost, Encoding encodinggbk, string ticketHtml, out string time)
        {
            if (string.IsNullOrEmpty(postData))
            {
                time = "";
                return GetHtml(url);
            }            try
            {
                // postData = System.Web.HttpUtility.UrlEncode(postData);
                byte[] byteRequest = encodinggbk.GetBytes(postData);                HttpWebRequest httpWebRequest = CreateWebRequestLhb(url);
                httpWebRequest.Method = isPost ? "POST" : "GET";                httpWebRequest.Referer = ticketHtml;
                DateTime strat = DateTime.Now;
                double streamRead = 0; double placet = 0;
                using (Stream stream = httpWebRequest.GetRequestStream())
                {
                    stream.Write(byteRequest, 0, byteRequest.Length);
                    streamRead = (DateTime.Now - strat).TotalMilliseconds;
                }
                strat = DateTime.Now;
                using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                {
                    double a1 = (DateTime.Now - strat).Milliseconds;
                    this._responsUri = httpWebResponse.ResponseUri;
                    if (httpWebResponse.ContentEncoding.IndexOf("gzip") > -1)
                    {
                        //Content-Encoding
                        strat = DateTime.Now;
                        using (Stream responseStream = new System.IO.Compression.GZipStream(httpWebResponse.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress))
                        {
                            double a2 = (DateTime.Now - strat).Milliseconds;
                            strat = DateTime.Now;
                            using (StreamReader streamReader = new StreamReader(responseStream, encodinggbk))
                            {
                                double a3 = (DateTime.Now - strat).TotalMilliseconds;
                                DateTime placeDt = DateTime.Now;
                                string html = streamReader.ReadToEnd();
                                placet = (DateTime.Now - placeDt).TotalMilliseconds;
                                time = streamRead + "-" + a1 + "-" + placet + "-" + a2 + "-" + a3;
                                return html;                            }
                        }
                    }
                    else if (httpWebResponse.ContentEncoding.IndexOf("deflate") > -1)
                    {
                        using (Stream responseStream = new System.IO.Compression.DeflateStream(httpWebResponse.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress))
                        {
                            using (StreamReader streamReader = new StreamReader(responseStream, encodinggbk))
                            {
                                time = "";
                                return streamReader.ReadToEnd();
                            }
                        }
                    }
                    else
                    {
                        using (Stream responseStream = httpWebResponse.GetResponseStream())
                        {
                            using (StreamReader streamReader = new StreamReader(responseStream, encodinggbk))
                            {
                                time = "";
                                return streamReader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
                Console.ForegroundColor = ConsoleColor.White;
                time = "";
                return string.Empty;
            }
        }private HttpWebRequest CreateWebRequestLhb(string url)
        {
            HttpWebRequest httpWebRequest;
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            httpWebRequest.CookieContainer = this.cookieContainer;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Referer = string.IsNullOrEmpty(this.Referer) ? url : this.Referer;
            httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*";// "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322)";
            httpWebRequest.Headers.Add("Cache-Control", "no-cache");
            httpWebRequest.KeepAlive = true;
            httpWebRequest.ContentLength = 213;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";            httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            httpWebRequest.AllowWriteStreamBuffering = false;
            httpWebRequest.Headers.Add("Accept-Language", "zh-cn");
            //httpWebRequest.Headers.Add("Accept-Charset", acceptCharSet);
            httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322)";            return httpWebRequest;
        }这句基本上占去了所有的时间,string html = streamReader.ReadToEnd();

解决方案 »

  1.   

    System.IO.Compression.GZipStream   
      

  2.   

    不要使用ReadToEnd,直接使用Stream.Read读取。实际上,凡是网络上的通讯,都不要使用ReadToEnd。
      

  3.   

    楼上是要Stream.Read循环读吗?
      

  4.   

    你应该把网站也贴出来
    也许设置一下responseStream.ReadTimeout = 10;就可以了
    你那一句string html = streamReader.ReadToEnd();出现了假死现象
      

  5.   


    能说下原因吗?期待原因ing
      

  6.   

    stream的好处就是边流边访问...你都ReadToEnd了干等着都流完再处理,stream对你也就毫无意义了...
      

  7.   

    FileStream 纠结,玩了好几天才能写入,你现在又玩读取,有点难度,建议:如果不会用FileStream还是用别的吧,太纠结了
      

  8.   


    你的说法太偏激了,如果我要的只是读取网页的源码,而读取过程中产生的stream我不用去处理,那又何必在乎stream呢?
    再者,如果读取的是小文件或很小的网页源码和不频繁处理的话,用ReadToEnd好;而大文件或频繁处理用read好
      

  9.   

    看来LZ还是不相信是假死,
    你想要结果,就加上下面这句试试吧,也许就是你要的结果了
    responseStream.ReadTimeout = 10;(默认是300000(即5分钟))
      

  10.   

    无人能解决的问题啊,现在想用c++或者delphi搞了