爲此,您可以簡單地在.net中使用「HttpWebRequest」和「HttpWebResponse」類。
下面是我寫的示例控制檯應用程序,演示了這是多麼容易。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string url = "www.somewhere.com/files/feed/list.zip";
string fileName = @"C:\list.zip";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 5000;
try
{
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
byte[] bytes = ReadFully(response.GetResponseStream());
stream.Write(bytes, 0, bytes.Length);
}
}
}
catch (WebException)
{
Console.WriteLine("Error Occured");
}
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
}
享受!
@ Reed:我已經在使用簡單的方法,我想知道和理解困難的方法。這基本上是簡單的方法的骨幹,糾正我,如果我錯了。 – 2010-10-06 23:24:12
@FábioAntunes:如果你想要「超越簡單的方式」,我編輯了我的答案,爲你提供這方面所需的指導。 – 2010-10-06 23:53:41
@Reed:任何建議,技巧,DOS和不要做,並且一個特殊的工具來監視流量感激? – 2010-10-07 01:32:12