c#获取网络源码

Posted on 2021-11-22 15:24  东方姑娘  阅读(41)  评论(0编辑  收藏  举报
   public static string GetHtml(string url)
    {
        //创建Httphelper对象
        HttpHelper http = new HttpHelper();
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;//创建Httphelper参数对象
        HttpItem item = new HttpItem()
        {
            URL = url,//URL     必需项
            Method = "GET",//URL     可选项 默认为Get
            Timeout = 100000,//连接超时时间     可选项默认为100000
            ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
            IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
            UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",//用户的浏览器类型,版本,操作系统     可选项有默认值
            Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            ContentType = "text/html",//返回类型    可选项有默认值
            AutoRedirectCookie = true//是否自动处理Cookie     可选项
        };
        //请求的返回值对象
        HttpResult result = http.GetHtml(item);
        string html = result.Html;
        return html;
    }

方法使用:

string url = "http://www.114390.com/lizhi/rng.html";
string html = NetClass.GetHtml(url);
Response.Write(html);
Response.End();

 

Copyright © 2024 东方姑娘
Powered by .NET 9.0 on Kubernetes