Krishna Kumar
Krishna Kumar

Reputation: 4884

Fastest C# Code to Download a Web Page

Given a URL, what would be the most efficient code to download the contents of that web page? I am only considering the HTML, not associated images, JS and CSS.

Upvotes: 65

Views: 79870

Answers (7)

Auto
Auto

Reputation: 676

Here you have a fast and short method to download files from Internet. It uses the new HttpClient object, new to .NET Framework 4.0+ and can be used also with .NET. It also uses the aync await.

async Task<string> CurlAsync(string url) => await new HttpClient().GetStringAsync(url).ConfigureAwait(false);

// usage:

var content = await CurlAsync(myURL);

Upvotes: 0

Amir Astaneh
Amir Astaneh

Reputation: 2226

I think this is the fastest (download speed time with low latency) solution for download.

// WebClient vs HttpClient vs HttpWebRequest vs RestSharp
// در نهایت به نظرم روش زیر سریعترین روشه
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
Request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
Request.Proxy = null;
Request.Method = "GET";
using (WebResponse Response = Request.GetResponse())
{
    using (StreamReader Reader = new StreamReader(Response.GetResponseStream()))
    {
        return Reader.ReadToEnd();
    }
}

Upvotes: 4

liang
liang

Reputation: 1611

WebClient.DownloadString

public static void DownloadString (string address)
{
    WebClient client = new WebClient ();
    string reply = client.DownloadString (address);

    Console.WriteLine (reply);
}

Upvotes: 10

EKanadily
EKanadily

Reputation: 3977

here is my answer ,a method that takes a URL and return a string

public static string downloadWebPage(string theURL)
    {
        //### download a web page to a string
        WebClient client = new WebClient();

        client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        Stream data = client.OpenRead(theURL);
        StreamReader reader = new StreamReader(data);
        string s = reader.ReadToEnd();
        return s;
    }

Upvotes: 8

Adam Haile
Adam Haile

Reputation: 31359

Use the WebClient class from System.Net; on .NET 2.0 and higher.

WebClient Client = new WebClient ();
Client.DownloadFile("http://mysite.com/myfile.txt", " C:\myfile.txt");

Upvotes: 23

John Sheehan
John Sheehan

Reputation: 78152

public static void DownloadFile(string remoteFilename, string localFilename)
{
    WebClient client = new WebClient();
    client.DownloadFile(remoteFilename, localFilename);
}

Upvotes: 71

Chris
Chris

Reputation: 6842

System.Net.WebClient

From MSDN:

using System;
using System.Net;
using System.IO;

public class Test
{
    public static void Main (string[] args)
    {
        if (args == null || args.Length == 0)
        {
            throw new ApplicationException ("Specify the URI of the resource to retrieve.");
        }
        WebClient client = new WebClient ();

        // Add a user agent header in case the 
        // requested URI contains a query.

        client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        Stream data = client.OpenRead (args[0]);
        StreamReader reader = new StreamReader (data);
        string s = reader.ReadToEnd ();
        Console.WriteLine (s);
        data.Close ();
        reader.Close ();
    }
}

Upvotes: 30

Related Questions