当前位置:编程学习 > 网站相关 >>

根据url获取网站HTML内容

/// <summary>  

/// 根据url获取网站HTML内容  

/// </summary>    /// <param name="url">网址</param>

/// <returns>获取网站HTML内容</returns>  

public string GetHtmlContentByUrl(string url)  

{    

  var htmlContent = string.Empty;      

  var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);      

  httpWebRequest.Timeout = 10000000;      

  var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();      

  var stream = httpWebResponse.GetResponseStream();

      if (stream != null)      

  {          

    var streamReader = new StreamReader(stream, System.Text.Encoding.UTF8);          

    htmlContent = streamReader.ReadToEnd();          

    streamReader.Close();          

    streamReader.Dispose();          

    stream.Close();          

    stream.Dispose();      

  }

      httpWebResponse.Close();      

  return htmlContent;  

}

 

摘自 期待光明前景
补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,