下载网页的最快 C# 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-02 10:53:46  来源:igfitidea点击:

Fastest C# Code to Download a Web Page

提问by Krishna Kumar

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.

给定一个 URL,下载该网页内容的最有效代码是什么?我只考虑 HTML,不考虑相关图像、JS 和 CSS。

采纳答案by John Sheehan

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

回答by Chris

System.Net.WebClient

系统.Net.Web客户端

From MSDN:

来自 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 ();
    }
}

回答by Adam Haile

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

使用 System.Net 中的 WebClient 类;在 .NET 2.0 及更高版本上。

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

回答by EKanadily

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

这是我的答案,一种采用 URL 并返回字符串的方法

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

回答by liang

WebClient.DownloadString

WebClient.DownloadString

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

    Console.WriteLine (reply);
}

回答by AmirMehdi KhademAstaneh

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