首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

网络资源下载,该如何处理

2012-01-26 
网络资源下载有一个HTTP 连接 我想用 C# 代码实现 下载这个 连接的文件 怎么做谢谢[解决办法]用WebClient:

网络资源下载
有一个HTTP 连接 我想用 C# 代码实现 下载这个 连接的文件 怎么做

谢谢

[解决办法]
用WebClient:
下面的代码示例接受资源的 URI,检索它,然后显示响应。
 

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

热点排行