c#获取网页代码、数据、资源

//WebClient取网页源码
private string GetHtmlSource(string Url)
{
    try
    {
        System.Net.WebClient wc = new System.Net.WebClient();
        wc.Headers["Accept-Language"] = "zh-cn";
        wc.Encoding = Encoding.GetEncoding("utf-8");

        return wc.DownloadString(Url);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return "";
    }
}

除了DownloadString,WebClient还有DownloadFile和DownloadData两个函数。可以下载数据和文件。而DownloadString可以下载uri对应的网页代码。

留文备用。

posted @ 2014-06-23 14:02  脸谱匠  阅读(551)  评论(0编辑  收藏  举报