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"));

相关文章:

  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-11-02
  • 2021-11-27
  • 2021-12-12
  • 2021-12-03
  • 2022-12-23
猜你喜欢
  • 2021-05-29
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
相关资源
相似解决方案