none
HttpWebRequest.GetRequestStream() 第一次访问速度慢问题 RRS feed

  • 问题

  • 我这段代码,如果第一次使用的时候在Stream writer = request.GetRequestStream();这句要卡住好长时间才放映。

    google找了一下说用winnet.dll,我又有点怕不稳定。在一个是cookie怎么记录也不知道。

    有没有什么办法可以让他快一点~!

    UTF8Encoding encoding = new UTF8Encoding();
            string postData = "u=" + u;
            postData += ("&p=" + p);
            byte[] data = encoding.GetBytes(postData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_Url);
            //request.Headers.Add(HttpRequestHeader.AcceptCharset, "GB2312,utf-8;q=0.7,*;q=0.7");
            request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn,zh;q=0.5");
            //request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            request.Method = "POST";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0";
            request.KeepAlive = true;
            request.Timeout = 60000;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cc;
            request.ContentLength = data.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            _content = reader.ReadToEnd();
    
            try
            {
              writer.Close();
              response.Close();
              request = null;
              response = null;
              writer = null;
            }
            catch { }
    
    • 已移动 Sheng Jiang 蒋晟Moderator 2011年5月29日 2:28 System.Net (发件人:Visual C#)
    • 已移动 Paul Zhou 2011年5月31日 6:02 (发件人:.NET Framework 一般性问题讨论区)
    2011年5月28日 21:52

答案

全部回复