Monday, June 15, 2020

C# download web page source

Download using System.Net.WebClient class

The easiest way to download an URL to file or string in C# is using the System.Net.WebClient class.
Download URL to file using WebClient class:
using System.Net;

WebClient wc = new WebClient();
wc.DownloadFile("http://www.example.com/somefile.txt", @"c:\temp\somefile.txt");
Download URL to string using WebClient class:
using System.Net;

WebClient wc = new WebClient();
string somestring = wc.DownloadString("http://www.example.com/somefile.txt");
The download function will throw a WebException if the download fails, so we need to put the function in a try {} block.
using System.Net;

string somestring;
try
{
    WebClient wc = new WebClient();
    somestring = wc.DownloadString("http://www.example.com/somefile.txt");
}
catch (WebException we)
{
    // add some kind of error processing
    MessageBox.Show(we.ToString());
}

System.Net.WebClient with Timeout

The WebClient class has no timeout property, but we can override GetWebRequest(Uri address) in our own derived class.
Please note, that the timeout property in GetWebRequest affects synchronous requests only - it will not work for asynchronous requests.
using System.Net;

public class WebClientWithTimeout:WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest wr = base.GetWebRequest(address);
        wr.Timeout = 5000; // timeout in milliseconds (ms)
        return wr;
    }
}

...

string somestring;
try
{
    WebClient wc = new WebClientWithTimeout();
    somestring = wc.DownloadString("http://www.example.com/somefile.txt");
}
catch (WebException we)
{
    // add some kind of error processing
    MessageBox.Show(we.ToString());
}


Server Error 500 (Internal Server Error)

The web server may return 500 (Internal Server Error) if the user agent header is missing.
Add "user-agent" to the http headers:
...
WebClient wc = new WebClient();
wc.Headers.Add ("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
somestring = wc.DownloadString("http://www.example.com/somefile.txt");
...


https://www.arclab.com/en/kb/csharp/download-file-from-internet-to-string-or-file.html

https://sourceforge.net/projects/delphiemail/

https://sourceforge.net/projects/phxmail/

No comments:

Post a Comment

Коментар: