using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DownloadImagebyXMLListFor2008 {
public class HttpDownLoad {
/*
*/
/// <summary>
/// HttpWebRequest Property
/// </summary>
/// <param name="fileName"></param>
/// <param name="url"></param>
/// <param name="localPath"></param>
/// <param name="timeout"></param>
public static void DownloadOneFileByURL(string fileName, string url, string localPath, int timeout){
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url + fileName);
request.Timeout = timeout;
//8000 Not work ?
response = (System.Net.HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
BinaryReader br = new BinaryReader(s);
//int length2 = Int32.TryParse(response.ContentLength.ToString(), out 0);
int length2 = Int32.Parse(response.ContentLength.ToString());
byte[] byteArr = new byte[length2];
s.Read(byteArr, 0, length2);
if (File.Exists(localPath + fileName)) {
File.Delete(localPath + fileName);
}
if (Directory.Exists(localPath) == false) {
Directory.CreateDirectory(localPath);
}
FileStream fs = File.Create(localPath + fileName);
fs.Write(byteArr, 0, length2);
fs.Close();
br.Close();
}
/**/
/// <summary>
/// Web Client Method ,only For Small picture
/// </summary>
/// <param name="fileName"></param>
/// <param name="url"></param>
/// <param name="localPath"></param>
public static void DownloadOneFileByURLWithWebClient(string fileName, string url, string localPath)
{
System.Net.WebClient wc = new System.Net.WebClient();
if (File.Exists(localPath + fileName)) {
File.Delete(localPath + fileName);
}
if (Directory.Exists(localPath) == false) {
Directory.CreateDirectory(localPath);
}
wc.DownloadFile(url + fileName, localPath + fileName);
}
}
}
需要注意点:
第一 DownloadOneFileByURL方法,有时会下载不了文件,如果文件大于40K就更明显, DownloadOneFileByURLWithWebClient则无此问题。
第二 调用时请用Thread,给出一个示例
private void btnGet_Click(object sender, EventArgs e){
if (txtTempFile.Text.Trim().Length == 0){
ErrorStop("列表文件为空!");
return;
}
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DownloadAll));
thread.Start();
}
private void DownloadAll(){
List <string> ls = GetStringsByFile(txtTempFile.Text.Trim());
if (null != ls){
foreach (string s in ls){
try{
//HttpDownLoad.DownloadOneFileByURL(s,Globals.HttpPreUrl, Globals.LocalPrePath, 8000000);
HttpDownLoad.DownloadOneFileByURLWithWebClient(s,Globals.HttpPreUrl, Globals.LocalPrePath);
}catch {
continue;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DownloadImagebyXMLListFor2008 {
public class HttpDownLoad {
/*
*/
/// <summary>
/// HttpWebRequest Property
/// </summary>
/// <param name="fileName"></param>
/// <param name="url"></param>
/// <param name="localPath"></param>
/// <param name="timeout"></param>
public static void DownloadOneFileByURL(string fileName, string url, string localPath, int timeout){
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url + fileName);
request.Timeout = timeout;
//8000 Not work ?
response = (System.Net.HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
BinaryReader br = new BinaryReader(s);
//int length2 = Int32.TryParse(response.ContentLength.ToString(), out 0);
int length2 = Int32.Parse(response.ContentLength.ToString());
byte[] byteArr = new byte[length2];
s.Read(byteArr, 0, length2);
if (File.Exists(localPath + fileName)) {
File.Delete(localPath + fileName);
}
if (Directory.Exists(localPath) == false) {
Directory.CreateDirectory(localPath);
}
FileStream fs = File.Create(localPath + fileName);
fs.Write(byteArr, 0, length2);
fs.Close();
br.Close();
}
/**/
/// <summary>
/// Web Client Method ,only For Small picture
/// </summary>
/// <param name="fileName"></param>
/// <param name="url"></param>
/// <param name="localPath"></param>
public static void DownloadOneFileByURLWithWebClient(string fileName, string url, string localPath)
{
System.Net.WebClient wc = new System.Net.WebClient();
if (File.Exists(localPath + fileName)) {
File.Delete(localPath + fileName);
}
if (Directory.Exists(localPath) == false) {
Directory.CreateDirectory(localPath);
}
wc.DownloadFile(url + fileName, localPath + fileName);
}
}
}
需要注意点:
第一 DownloadOneFileByURL方法,有时会下载不了文件,如果文件大于40K就更明显, DownloadOneFileByURLWithWebClient则无此问题。
第二 调用时请用Thread,给出一个示例
private void btnGet_Click(object sender, EventArgs e){
if (txtTempFile.Text.Trim().Length == 0){
ErrorStop("列表文件为空!");
return;
}
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DownloadAll));
thread.Start();
}
private void DownloadAll(){
List <string> ls = GetStringsByFile(txtTempFile.Text.Trim());
if (null != ls){
foreach (string s in ls){
try{
//HttpDownLoad.DownloadOneFileByURL(s,Globals.HttpPreUrl, Globals.LocalPrePath, 8000000);
HttpDownLoad.DownloadOneFileByURLWithWebClient(s,Globals.HttpPreUrl, Globals.LocalPrePath);
}catch {
continue;
}
}
}
}