using System; using System.Net;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { WebClient webClient = new WebClient(); webClient.DownloadFile(url, path); Console.ReadKey(); } } }
using System.Net; using System.IO; /// <summary> /// 下载服务器至客户端 /// </summary> /// <param name="URL">被下载的地址, 绝对路径</param> /// <param name="Dir">另存放的目录</param> public static void Download(string URL, string Dir) { WebClient client = new WebClient(); string fileName = URL.Substring(URL.LastIndexOf("/") + 1); //被下载的名 string Path = Dir + fileName; //另存为的绝对路径+名 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); request.Method = "GET"; //request.Timeout = 300000; request.KeepAlive = true; request.AllowAutoRedirect = true; request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)"; request.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse webresponse = null; try { webresponse = request.GetResponse() as HttpWebResponse; if (webresponse != null) { Stream st = webresponse.GetResponseStream(); Stream so = new FileStream(Path, FileMode.Create); byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); } } catch (System.Exception e) { } finally { if (webresponse != null) { webresponse.Close(); webresponse = null; } if (request != null) { request.Abort(); request = null; } } }
using System.Net;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
webClient.DownloadFile(url, path); Console.ReadKey();
}
}
}
using System.Net;
using System.IO; /// <summary>
/// 下载服务器至客户端
/// </summary>
/// <param name="URL">被下载的地址, 绝对路径</param>
/// <param name="Dir">另存放的目录</param>
public static void Download(string URL, string Dir)
{
WebClient client = new WebClient();
string fileName = URL.Substring(URL.LastIndexOf("/") + 1); //被下载的名
string Path = Dir + fileName; //另存为的绝对路径+名
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET"; //request.Timeout = 300000;
request.KeepAlive = true;
request.AllowAutoRedirect = true; request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)"; request.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse webresponse = null;
try
{
webresponse = request.GetResponse() as HttpWebResponse;
if (webresponse != null)
{
Stream st = webresponse.GetResponseStream();
Stream so = new FileStream(Path, FileMode.Create);
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
}
}
catch (System.Exception e)
{
}
finally
{
if (webresponse != null)
{
webresponse.Close();
webresponse = null;
}
if (request != null)
{
request.Abort();
request = null;
}
} }