Fastest C # code to load a web page

Given the URL, what would be the most efficient code to load the contents of this web page? I am only looking at HTML, not related images, JS and CSS.

+52
c #
Aug 25 '08 at 15:23
source share
6 answers
public static void DownloadFile(string remoteFilename, string localFilename) { WebClient client = new WebClient(); client.DownloadFile(remoteFilename, localFilename); } 
+60
Aug 25 '08 at 15:24
source share

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 (); } } 
+25
Aug 25 '08 at 15:24
source share

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"); 
+23
Aug 25 '08 at 15:25
source share

here is my answer, a method that takes a url and returns 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; } 
+6
Sep 01 '14 at 11:46
source share

WebClient.DownloadString

 public static void DownloadString (string address) { WebClient client = new WebClient (); string reply = client.DownloadString (address); Console.WriteLine (reply); } 
+5
Dec 18 '14 at 17:39
source share

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

 // 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(); } } 
0
Feb 03 '19 at 11:40
source share



All Articles