zoukankan      html  css  js  c++  java
  • 关于StreamReader.ReadToEnd方法

        以前写抓取网页的代码喜欢用ReadToEnd,因为简单省事,后来发现,在爬取网页的时候,如果网速很慢,ReadToEnd超时的几率很大。使用Read改写后,超时几率大大减小,完整代码如下:

    /// <summary>
    /// HttpPost
    /// </summary>
    public static string HttpPost(string url, string data)
    {
        byte[] bArr = ASCIIEncoding.UTF8.GetBytes(data);
    
        // 设置参数
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.CookieContainer = m_Cookie;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = bArr.Length;
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)";
    
        Stream postStream = request.GetRequestStream();
        postStream.Write(bArr, 0, bArr.Length);
        postStream.Close();
    
        //发送请求并获取相应回应数据
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        //直到request.GetResponse()程序才开始向目标网页发送Post请求
        Stream responseStream = response.GetResponseStream();
        //返回结果网页(html)代码
        MemoryStream memoryStream = new MemoryStream();
        bArr = new byte[1024];
        int size = responseStream.Read(bArr, 0, (int)bArr.Length);
        while (size > 0)
        {
            memoryStream.Write(bArr, 0, size);
            size = responseStream.Read(bArr, 0, (int)bArr.Length);
            Thread.Sleep(1);
        }
        string content = Encoding.UTF8.GetString(memoryStream.ToArray());
        return content;
    }
    View Code

     代码中Thread.Sleep(1);也可以去掉。

  • 相关阅读:
    Zend Studio下调试PHP的一点注意事项
    使用FTP出现中文乱码解决方案
    C++数据类型与C#对应表
    FineUI 开发B/S系统
    c#调用带输出参数的存储过程
    经典FormsAuthenticationTicket 分析
    C# Oracle数据库操作类实例详解
    如何使用C#的Hashtable[哈希表]
    对三层架构的理解
    C# 最简单的三层架构实例 ——转载自网易博客
  • 原文地址:https://www.cnblogs.com/s0611163/p/7246113.html
Copyright © 2011-2022 走看看