关于StreamReader.ReadToEnd方法_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 关于StreamReader.ReadToEnd方法

关于StreamReader.ReadToEnd方法

 2017/7/28 5:31:04  秋荷雨翔  程序员俱乐部  我要评论(0)
  • 摘要:以前写抓取网页的代码喜欢用ReadToEnd,因为简单省事,后来发现,在爬取网页的时候,如果网速很慢,ReadToEnd超时的几率很大。使用Read改写后,超时几率大大减小,完整代码如下:///<summary>///HttpPost///</summary>publicstaticstringHttpPost(stringurl,stringdata){byte[]bArr=ASCIIEncoding.UTF8.GetBytes(data)
  • 标签:方法

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

class="code_img_closed" src="/Upload/Images/2017072805/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('551ee29f-798b-4105-a684-7484e9af7b63',event)" src="/Upload/Images/2017072805/2B1B950FA3DF188F.gif" alt="" />
/// <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);也可以去掉。

发表评论
用户名: 匿名