【问题标题】:how to get html page source by C#如何通过C#获取html页面源代码
【发布时间】:2017-06-06 07:03:05
【问题描述】:

我想通过.htmurlurl将完整的网页asp保存在本地驱动器中,但我没有成功。

代码

public StreamReader Fn_DownloadWebPageComplete(string link_Pagesource)
{
     //--------- Download Complete ------------------
     //  using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
     //   {

     //client
     //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(link_Pagesource);

                    //webRequest.AllowAutoRedirect = true;
                    //var client1 = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(link_Pagesource);
                    //client1.CookieContainer = new System.Net.CookieContainer();


                 //   client.DownloadFile(link_Pagesource, @"D:\S1.htm");

              //  }
         //--------- Download Page Source ------------------
 HttpWebRequest URL_pageSource = (HttpWebRequest)WebRequest.Create("https://www.digikala.com");

                    URL_pageSource.Timeout = 360000;
                    //URL_pageSource.Timeout = 1000000;
                    URL_pageSource.ReadWriteTimeout = 360000;
                   // URL_pageSource.ReadWriteTimeout = 1000000;
                    URL_pageSource.AllowAutoRedirect = true;
                    URL_pageSource.MaximumAutomaticRedirections = 300;

                    using (WebResponse MyResponse_PageSource = URL_pageSource.GetResponse())
                    {

                        str_PageSource = new StreamReader(MyResponse_PageSource.GetResponseStream(), System.Text.Encoding.UTF8);
                        pagesource1 = str_PageSource.ReadToEnd();
                        success = true;
                    }

错误:

尝试了过多的自动重定向。

通过此代码尝试但不成功。

许多 url 使用此代码成功,但此 url 不成功。

【问题讨论】:

    标签: c#


    【解决方案1】:

    这里是方法

    string url = "https://www.digikala.com/";
    
    using (HttpClient client = new HttpClient())
    {
       using (HttpResponseMessage response = client.GetAsync(url).Result)
       {
          using (HttpContent content = response.Content)
          {
             string result = content.ReadAsStringAsync().Result;
          }
       }
    }
    

    result 变量将包含HTML 的页面,然后您可以将其保存到这样的文件中

    System.IO.File.WriteAllText("path/filename.html", result);
    

    注意你必须使用命名空间

    using System.Net.Http;
    

    更新如果您使用的是旧版 VS,那么您可以看到 answer 用于相同目的使用 WebClientWebRequest,但实际上更新您的 VS 是一个更好的解决方案。

    【讨论】:

    • 谢谢,必须更新到vs2013并安装System.Net.Http并测试。
    • @RedArmy 很高兴为您提供帮助,如果解决了您的问题,请考虑接受此答案。
    • 虽然使用.Result 确实有效,但它将异步调用转换为阻塞同步调用。最好 await 异步方法调用(此时无需使用 .Result)从异步特性中受益。
    • @HansKesting 您是正确的,但这取决于用户本身的要求。无论如何,我在我链接的其他扩展答案中解决了这个问题。您可以考虑使用扩展答案来处理HttpClient 的大多数情况
    • 亲爱的@HakamFostok 保存源代码的好解决方案,但是这个网站使用,ajax 等来展示产品,请参阅 url
    【解决方案2】:
    using (WebClient client = new WebClient ())
    {
        client.DownloadFile("https://www.digikala.com", @"C:\localfile.html");
    }
    

    【讨论】:

    • 感谢@imbot359,但使用此代码并不适用于此链接。我不知道为什么。
    【解决方案3】:
    using (WebClient client = new WebClient ())
    {
        string htmlCode = client.DownloadString("https://www.digikala.com");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 2014-06-07
      • 1970-01-01
      • 2016-01-08
      相关资源
      最近更新 更多