在C#中下载HTML页面

6

我正在用C#编写一个应用程序,有没有一种方法只通过给我的程序URL来下载HTML页面。例如,我的程序将获取URL www.google.com 并下载HTML页面?

2个回答

17

1
避免这个。它非常慢(即使使用proxy=null的技巧)。 - Filipe YaBa Polido
这个 Filipe 的替代方案是什么? - Viking

8
使用WebClient类。
这段内容摘自msdn文档页面上的示例:链接
using System;
using System.Net;
using System.IO;

public static string Download (string uri)
{
    WebClient client = new WebClient ();

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

使用一些using语句怎么样?上面的代码没有处理WebClient对象的释放。 - Adam White

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接