C#下载http文件
@(编程)
using System;
using System.IO;
using System.Net;
namespace Wisdombud.Util
{
public class HttpHelper
{
public static bool DownloadFile(string localFile, string webFile)
{
bool flag = false;
long SPosition = 0;
FileStream FStream;
if (File.Exists(localFile))
{
File.Delete(localFile);
FStream = File.OpenWrite(localFile);
SPosition = FStream.Length;
FStream.Seek(SPosition, SeekOrigin.Current);
}
else
{
FStream = new FileStream(localFile, FileMode.Create);
SPosition = 0;
}
try
{
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(webFile);
if (SPosition > 0)
myRequest.AddRange((int)SPosition);
Stream myStream = myRequest.GetResponse().GetResponseStream();
byte[] btContent = new byte[512];
int intSize = 0;
intSize = myStream.Read(btContent, 0, 512);
while (intSize > 0)
{
FStream.Write(btContent, 0, intSize);
intSize = myStream.Read(btContent, 0, 512);
}
FStream.Close();
myStream.Close();
flag = true;
}
catch (Exception)
{
FStream.Close();
flag = false;
}
return flag;
}
}
}