【问题标题】:Download HTML Page in C#用 C# 下载 HTML 页面
【发布时间】:2011-10-30 10:29:54
【问题描述】:

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

【问题讨论】:

    标签: c# html url


    【解决方案1】:

    使用 WebClient 类。

    这是从 msdn doc page 上的示例中提取的:

    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。
    【解决方案2】:

    【讨论】:

    • 避免这种情况。这很痛苦 SLOWWWWWWWWWWWWWWWW(即使使用 proxy=null 技巧)
    • 这个 Filipe 的替代品?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多