zoukankan      html  css  js  c++  java
  • WebRequest 后台访问网页内容

    1 方法封装: 

     public static class WebFunc
        {

    public static string GetHtml(string url, Encoding encoding)
            {
                string html = string.Empty;
                try
                {
                    WebRequest request;
                    request = WebRequest.Create(url);
                    request.Credentials = CredentialCache.DefaultCredentials;
                    request.Timeout = 20000;
                    WebResponse response;
                    response = request.GetResponse();
                    html = new StreamReader(response.GetResponseStream(), encoding).ReadToEnd();
                }
                catch(System.UriFormatException uex)
                {
                    LogHelper.Error(string.Format("ex:{0}, url:{1}", uex, url));
                }
                catch (System.Net.WebException ex)
                {
                    LogHelper.Error(string.Format("ex:{0}, url:{1}", ex, url));
                }
                return html;
            }

    }

    调用:  string contents = WebFunc.GetHtml(url,Encoding.GetEncoding("gb2312"));

    作者:沐雪
    文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
    如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
    找一找教程网-随时随地学软件编程 http://www.zyiz.net/

  • 相关阅读:
    后台跨域(CORS)
    golang 处理TCP粘包问题
    使用axios 发送ajax 下载文件
    Golang:在Redigo的RedisPool上选择DB
    puppeteer添加代理
    mongodb 权限操作
    alpine下安装icu-dev
    golang 导出CSV文件中文乱码的问题
    shell笔记
    Convert rune to int
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628977.html
Copyright © 2011-2022 走看看