Сведения о вопросе

nikolya

20:45, 5th August, 2020

Теги

c#    

Самый быстрый C# код для загрузки веб-страницы

Просмотров: 387   Ответов: 5

Учитывая URL, какой код будет наиболее эффективным для загрузки содержимого этой веб-страницы? Я рассматриваю только HTML, а не связанные образы, JS и CSS.



  Сведения об ответе

PAGE

14:00, 9th August, 2020

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


  Сведения об ответе

SEEYOU

02:49, 10th August, 2020

System.Net.WebClient

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


  Сведения об ответе

PIRLO

02:53, 8th August, 2020

Используйте класс WebClient от System.Net; на .NET 2.0 и выше.

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


  Сведения об ответе

park

20:22, 13th August, 2020

вот мой ответ, метод, который берет 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;
    }


  Сведения об ответе

DINO

12:34, 6th August, 2020

WebClient.DownloadString

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

    Console.WriteLine (reply);
}


Ответить на вопрос

Чтобы ответить на вопрос вам нужно войти в систему или зарегистрироваться