自己偶尔会去replays.net上下载一些rep来看(比较喜欢打魔兽)所以希望有一个工具可以把replays.net上的比较精彩的rep给down下来,于是乎做了一个小小的东西。设计思想比较简单,写起来也不麻烦。
用webRequest对replays.net发出请求,用webResponse获得网站服务器发回的回复,转换成为stream,用一个StreamReader将steam逐行读出,从中找到存放我们感兴趣的rep的网页的url存到ArrayList中,然后分别对ArrayList中的url发送请求,用一个简单的WebClient.DownLoadFile(string url, string filepath)就可以将其下载到本地的filepath中。看到网上有将网站发回的数据转换成stream然后将stream写成一个文件的做法,个人认为也是可取的。只不过有了现成的函数就偷了个懒了(希望不要拍砖@#¥)。建议重新启一个线程来下载文件,因为考虑到网络的不稳定性,当一个url上的文件响应时间超时可能会造成该线程的长时间阻塞,从而大大降低了下载的速度。
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using System.Collections;
using System.Threading;
![](/Images/OutliningIndicators/None.gif)
namespace RepalysDownload_Beta
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] nums =
{ 2, 3, 4, 5, 6, 8, 9, 10, 12, 16 };
foreach (int num in nums)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Creat a webrequest
WebRequest wRequest = WebRequest.Create(LocalParameters.repwebUrl + num.ToString());
//Creat a webresponse to get the resonponse from the remote server
WebResponse wResponse = wRequest.GetResponse();
//Convert the response to stream.
Stream stream = wResponse.GetResponseStream();
StreamReader sr = new StreamReader(stream);
![](/Images/OutliningIndicators/InBlock.gif)
ArrayList AL = new ArrayList();
Replays rTemp = new Replays();
string strLine = null;
//The following cycle function is to analyzing the html and got the url that contains the reps we are interested in
//while ((strLine = sr.ReadLine()) != null)
//{
// if (strLine.StartsWith("<ul class=\"datarow3\""))
// //if(strLine.StartsWith("<li class=\"c_p\">"))
// {
// getUrlFromHtml getUrl = new getUrlFromHtml(strLine, "/doc", "html", 4);
// rTemp.url = getUrl.url();
// }
// if (strLine.StartsWith("<li class=\"c_i\">"))
// {
// if (strLine.Length > 21)
// {
// rTemp.hasCrown = true;
// AL.Add(rTemp);
// }
// }
//}
while ((strLine = sr.ReadLine()) != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (strLine.StartsWith("<li class=\"c_p\">"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
getUrlFromHtml getUrl = new getUrlFromHtml(strLine, "/doc", "html", 4);
rTemp.url = getUrl.url();
if (strLine.Contains("http://w3g.replays.net/images/"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rTemp.hasCrown = true;
AL.Add(rTemp);
}
}
}
foreach (Replays rep in AL)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WebRequest wbRequest = WebRequest.Create(LocalParameters.webUrl + rep.url);
WebResponse wbResponse = wbRequest.GetResponse();
Stream sstream = wbResponse.GetResponseStream();
StreamReader sReader = new StreamReader(sstream);
//The following cycle function is to analyzing the url to get the rep's url.
while ((strLine = sReader.ReadLine()) != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month.ToString();
string date = Convert.ToString((Convert.ToInt32(DateTime.Now.Day.ToString()) - 1));
if (strLine.Contains("Download REP"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
getUrlFromHtml gUrl = new getUrlFromHtml(strLine, "/Download.aspx?", ".w3g", 4);
string htmlUrl = gUrl.url();
string downLoadUrl = LocalParameters.webUrl + HttpUtility.UrlDecode(htmlUrl);
string[] downLoadUrlParts = downLoadUrl.Split('/');
int len = downLoadUrlParts.Length;
string repName = downLoadUrlParts[len - 1];
string dDate = year + "-" + month + "\\" + date + "\\";
if (downLoadUrl.Contains(year) && downLoadUrl.Contains(month) && downLoadUrl.Contains(date))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Directory.Exists(LocalParameters.repDownLoadTo + dDate))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.CreateDirectory(LocalParameters.repDownLoadTo + dDate);
}
WebClient wc = new WebClient();
DownLoadFile DLfile = new DownLoadFile(downLoadUrl, LocalParameters.repDownLoadTo + dDate + repName);
Thread tDownload = new Thread(new ThreadStart(DLfile.Download));
tDownload.Start();
}
}
}
}
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(ex.ToString());
}
}
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// This class is to Download the file from url to the path 'name'
/// </summary>
class DownLoadFile
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string url;
private string name;
public DownLoadFile(string Url, string Name)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.url = Url;
this.name = Name;
}
public void Download()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WebClient wc = new WebClient();
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
wc.DownloadFile(url, name);
Console.WriteLine("One rep has been download");
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Error:{0}", ex.ToString());
}
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Define the local parameters
/// </summary>
static class LocalParameters
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static string webUrl = "http://w3g.replays.net";
public static string repwebUrl = "http://w3g.replays.net/replaylist.aspx?Gamerace=";
public static string repDownLoadTo = @"E:\game\WarIII\魔兽争霸3冰封王座V1.21版\Warcraft III\replay\";
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// This struct is to store the url we're interested in
/// if the url is what we want the hasCrown equals 'true'
/// </summary>
struct Replays
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public string url;
public bool hasCrown;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The class is to get the url from replays list. the tip is to correct the index of end char
/// for example end with 'html' will set the tip as 4 while 'htm' as 3
/// </summary>
class getUrlFromHtml
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string urlInHtml;
private string begin;
private string end;
private int tip;
![](/Images/OutliningIndicators/InBlock.gif)
public getUrlFromHtml(string html, string beginWith, string endWith, int tip)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.urlInHtml = html;
this.begin = beginWith;
this.end = endWith;
this.tip = tip;
}
public string url()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int b = urlInHtml.IndexOf(begin);
int e = urlInHtml.IndexOf(end);
string url = urlInHtml.Substring(b, e - b + tip);
return url;
}
}
}
![](/Images/OutliningIndicators/None.gif)
附件已经编译好的文件
https://files.cnblogs.com/ppchouyou/ReplaysDownLoad.zip