RedArmy
RedArmy

Reputation: 475

how to get html page source by C#

I want to save complete web page asp in local drive by .htm from url or url but I did not success.

Code

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;
    }
}

Error :

Too many automatic redirections were attempted.

Attemp by this codes but not successful.

many url is successful with this codes but this url not successful.

Upvotes: 16

Views: 39476

Answers (3)

Hakan Fıstık
Hakan Fıstık

Reputation: 19401

Here is the way:

using System.Net.Http;

HttpClient client = new HttpClient();
string page = await client.GetStringAsync("https://stackoverflow.com/");

Upvotes: 25

user7450744
user7450744

Reputation:

using (WebClient client = new WebClient ())
{
    client.DownloadFile("https://www.digikala.com", @"C:\localfile.html");
}

Upvotes: 4

user7450788
user7450788

Reputation:

using (WebClient client = new WebClient ())
{
    string htmlCode = client.DownloadString("https://www.digikala.com");
}

Upvotes: 5

Related Questions