zoukankan      html  css  js  c++  java
  • [转][C#]抓取网页内容

    一个很简单的需求,关键代码如下:

    private string GetHtml(string item)
    {
        // set remote certificate Validation auto pass
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(remoteCertificateValidate);
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    
        string webSite = item;
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(webSite);
        req.KeepAlive = false;
        req.ProtocolVersion = HttpVersion.Version11;
        //req.AllowAutoRedirect = false;   // 不允许自动重定向
        req.Method = "GET";
        req.Timeout = 15 * 1000;    // 传入是秒,需要转换成毫秒
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";
    
        HttpWebResponse rsp = null;
        try
        {
            rsp = (HttpWebResponse)req.GetResponse();
            if (HttpStatusCode.OK == rsp.StatusCode)
            {
                Stream rspStream = rsp.GetResponseStream();     // 响应内容字节流
                using (StreamReader sr = new StreamReader(rspStream,Encoding.UTF8))
                {
                    return sr.ReadToEnd();
                }
            }
    
        }
        finally
        {
            rsp.Close();
        }
        return null;
    }
    private static bool remoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
    {
        return true;
    }
  • 相关阅读:
    Nowcoder9981A.串(排列组合)
    267D.Fedor and Essay(强连通分量缩点+DAG上DP)
    1290C. Prefix Enlightenment(带权并查集)
    LeetCode1. 两数之和
    LeetCode451. 根据字符出现频率排序
    LeetCode205. 同构字符串
    LeetCode290. 单词规律
    LeetCode202. 快乐数
    LeetCode242. 有效的字母异位词
    LeetCode136. 只出现一次的数字
  • 原文地址:https://www.cnblogs.com/z5337/p/12984929.html
Copyright © 2011-2022 走看看