1、使用HttpWebRequest/HttpWebResonse和WebClient
if (!response.ContentType.ToLower().StartsWith("text/"))
{
//Value = SaveBinaryFile(response, FileName);
byte[] buffer = new byte[1024];
Stream outStream = System.IO.File.Create(FileName);
Stream inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
}
while (l > 0);
outStream.Close();
inStream.Close();
}
</div>
2、使用WebClient
3、异步下载例子
#region 分析计时开始
count = 0;
count1 = 0;
freq = 0;
result = 0;
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
#endregion
using (WebClient wClient = new WebClient())
{
AutoResetEvent waiter = new AutoResetEvent(false);
wClient.Credentials = CredentialCache.DefaultCredentials;
wClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(AsyncURIAnalyze);
wClient.DownloadDataAsync(new Uri(uriString), waiter);
waiter.WaitOne(); 阻止当前线程,直到收到信号
}
}
///summary
///异步分析
///summary
protected void AsyncURIAnalyze(Object sender, DownloadDataCompletedEventArgs e)
{
AutoResetEvent waiter = (AutoResetEvent)e.UserState;
try
{
if (!e.Cancelled && e.Error == null)
{
string dnDir = string.Empty;
string domainName = string.Empty;
string uri = uriString;
获得域名 [url]httpwww.sina.com[url]
Match match = Regex.Match(uri, @((http(s)))+[w-.]+[^]);, RegexOptions.IgnoreCase
domainName = match.Value;
获得域名最深层目录 [url]httpwww.sina.commail[url]
if (domainName.Equals(uri))
dnDir = domainName;
else
dnDir = uri.Substring(0, uri.LastIndexOf(''));
dnDir += '';
获取数据
string pageData = Encoding.UTF8.GetString(e.Result);
Liststring urlList = new Liststring();
匹配全路径
match = Regex.Match(pageData, @((http(s)))+((()+[w-.]+()))+[w-.]+.+( + ImageType + )); , RegexOptions.IgnoreCase
&n