Reputation: 475
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
Reputation: 19401
Here is the way:
using System.Net.Http;
HttpClient client = new HttpClient();
string page = await client.GetStringAsync("https://stackoverflow.com/");
Upvotes: 25
Reputation:
using (WebClient client = new WebClient ())
{
client.DownloadFile("https://www.digikala.com", @"C:\localfile.html");
}
Upvotes: 4
Reputation:
using (WebClient client = new WebClient ())
{
string htmlCode = client.DownloadString("https://www.digikala.com");
}
Upvotes: 5