Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用C语言获取html页面源代码#_C# - Fatal编程技术网

C# 如何用C语言获取html页面源代码#

C# 如何用C语言获取html页面源代码#,c#,C#,我想通过或的.htm将完整的网页asp保存在本地驱动器中,但未成功 代码 public StreamReader Fn_DownloadWebPageComplete(string link_Pagesource) { //--------- Download Complete ------------------ // using (WebClient client = new WebClient()) // WebClient class inherits IDispo

我想通过或的
.htm
将完整的网页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不成功。

方法如下

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,那么您可以在出于相同目的使用
WebClient
WebRequest
时看到这一点,但实际上更新VS是一个更好的解决方案。

谢谢,必须更新到vs2013并安装System.Net.Http和测试。@RedArmy很高兴帮助您,如果解决了你的问题,请考虑接受这个答案。在使用<代码>。结果< /代码>工作时,它将异步调用转换成阻塞同步调用。最好等待异步方法调用(无需使用.Result-then),以从异步特性中获益。@HansKesting您的方法是正确的,但这取决于用户本身的要求。无论如何,我在我链接的另一个扩展答案中解决了这个问题。您可以考虑扩展的答案来处理大多数代码<代码> HTTPclipse < /代码>保存源代码亲爱的@ HakamFostok的好解决方案,但是这个网站使用的是Ajax等,以显示产品,请参阅URLYRESH @ IMBOT359,但使用此代码,不为这个链接工作。我不知道为什么。
using (WebClient client = new WebClient ())
{
    string htmlCode = client.DownloadString("https://www.digikala.com");
}
using (WebClient client = new WebClient ())
{
    client.DownloadFile("https://www.digikala.com", @"C:\localfile.html");
}