using System.IO;
using System.Text;
// 方法一:
// Create a request for the URL.
WebRequest request = WebRequest.Create( " http://www.hao123.com " );
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
Response.Write(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream,Encoding.Default);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Response.Write(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
// 方法二:
WebClient client = new WebClient();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add( " user-agent " , " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;) " );
Stream data = client.OpenRead( " http://www.hao123.com " );
StreamReader reader = new StreamReader(data,Encoding.Default);
string s = reader.ReadToEnd();
Response.Write(s);
data.Close();
reader.Close();
// 方法三:
WebClient client = new WebClient();
// client.DownloadFile(" http://www.hao123.com ","123.htm");
string reply = client.DownloadString( " http://www.hao123.com " );
Response.Write(reply);
---一般我用第三种
protected DataTable CatchIsues()
{
int IStart = 0;
int ILen = 0;
//""120108、120109、120110、120111 期号格式
string Isuse = "120110";
string Html = string.Empty;
string Error = string.Empty;
string[] Trs = null;
string[] Tds = null;
string[] Days = null;
string Date = string.Empty;
string League = string.Empty;
string MainTeam = string.Empty;
string GuestTeam = string.Empty;
string MatchDate = string.Empty;
string LoseBall = string.Empty;
string Color = string.Empty;
int count = 1;
DateTime dtTime = DateTime.Now;
DateTime MatchDateTime = DateTime.Now;
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(System.String));
table.Columns.Add("League", typeof(System.String));
table.Columns.Add("HostTeam", typeof(System.String));
table.Columns.Add("QuestTeam", typeof(System.String));
table.Columns.Add("loseBall", typeof(System.String));
table.Columns.Add("Time", typeof(System.String));
table.Columns.Add("color", typeof(System.String));
System.Net.WebClient wc = new System.Net.WebClient();
Html = GetHtml("http://zc.trade.500wan.com/bjdc/index.php?msgmode=2&expect=" + Isuse + "&jumphost=http://www.500wan.com", false);//http://trade.500wan.com/pages/info/bjdc/zc.php?msgmode=2&expect=" + Isuse + "&jumphost=http://www.500wan.com
string html = wc.DownloadString("http://zc.trade.500wan.com/bjdc/index.php?msgmode=2&expect=" + Isuse + "&jumphost=http://www.500wan.com").Replace("\r", "").Replace("\n", "");
Html = Html.Replace("\r", "").Replace("\n", ""); ;
//GetStrScope(Html, "<DIV CLASS=\"dc_l_m\"", "<A TITLE=\"回到顶部\"", out IStart, out ILen);
//Html = Html.Substring(IStart, ILen);
//if (ILen == 0)
//{
// Error = "抓取500万单场期号截取字符串失败";
//}
//<TR CLASS="vs_lines" STYLE="display:" VALUE="{index:'1',leagueName:'意甲',homeTeam:'卡塔尼亚',guestTeam:'帕尔玛',endTime:'2012-01-29 00:50',rangqiuNum:'0',scheduleDate:'2012-01-28',disabled:'yes',homeTeamRank:15,guestTeamRank:11,bgColor:'#0066FF'}">
// <TR CLASS="vs_lines even" STYLE="display:" VALUE="{index:'63',leagueName:'比乙',homeTeam:'登德',guestTeam:'安特卫普',endTime:'2012-01-29 21:20',rangqiuNum:'0',scheduleDate:'2012-01-29',disabled:'yes',homeTeamRank:14,guestTeamRank:6,bgColor:'#FDBA80'}">
// string strtable = Regex.Match(Html, @"<TR CLASS=""vs_lines even"">(.*?)</TR>").Groups[1].Value;
MatchCollection mc = Regex.Matches(Html, @"<TR [CLASS=""vs_lines even|CLASS=""vs_lines].*?>(\s*<td.*?>.*?</td>)+\s*</TR>", RegexOptions.IgnoreCase);
//MatchCollection mc = Regex.Matches(Html, @"<TR [CLASS=""vs_lines even""|CLASS=""vs_lines""]{1}.*?>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
Regex re1 = new Regex(@"value=""{.*?}");
Color = re1.Match(m.Groups[0].Captures[0].Value).Groups[0].Value;
if (Color != "")
{
int lenth = Color.Length;
string temp = Color.Substring(Color.IndexOf('{')+1, lenth - Color.IndexOf('{')-2).Trim();
string[] strTemp = temp.Split(',');
List<string> list = new List<string> { };
foreach (string s in strTemp)
{
list.Add(s.Split(':')[1].ToString());
}
if (list.Count > 0)
{
DataRow DRow = table.NewRow();
DRow["id"] = (count++).ToString();
DRow["League"] = list[0];
DRow["HostTeam"] = list[1];
DRow["QuestTeam"] = list[2];
DRow["loseBall"] = list[3];
DRow["Time"] = list[4];
DRow["Color"] = list[5];
table.Rows.Add(DRow);
}
}
}
//Days = Html.Split(new string[] { "<TR CLASS=\"vs_lines even\"" }, StringSplitOptions.RemoveEmptyEntries);
//for (int i = 0; i < Days.Length; i++)
//{
// Date = Days[i].Split(new string[] { "STYLE=\"display:\" VALUE=\"" }, StringSplitOptions.RemoveEmptyEntries)[0];
// Date = GetDate(Date);
// Trs = Days[i].Split(new string[] { "STYLE=\"display:\" VALUE=\"" }, StringSplitOptions.RemoveEmptyEntries);
// for (int j = 0; j < Trs.Length; j++)
// {
// Tds = Trs[j].Split(new string[] { "</td>" }, StringSplitOptions.RemoveEmptyEntries);
// if (Tds.Length == 19 || Tds.Length == 20)
// {
// League = getHtmlText(Tds[1], 3);
// GetStrScope(Tds[1], "bgcolor=", ">", out IStart, out ILen);
// Color = Tds[1].Substring(IStart, ILen).Replace("bgcolor=", "").Replace("\"", "");
// MatchDate = Date + " " + GetTime(Tds[2]);
// MainTeam = getHtmlText(Tds[4], 2);
// LoseBall = getHtmlText(Tds[5], 2);
// LoseBall = LoseBall.Length == 0 ? getHtmlText(Tds[5], 4) : LoseBall;
// GuestTeam = getHtmlText(Tds[6], 2);
// MatchDateTime = Shove._Convert.StrToDateTime(MatchDate + ":00", DateTime.Now.ToString());
// if (MatchDateTime.CompareTo(dtTime) < 0)
// {
// MatchDateTime = MatchDateTime.AddDays(1);
// }
// DataRow DRow = table.NewRow();
// DRow["id"] = (count++).ToString();
// DRow["League"] = League;
// DRow["HostTeam"] = MainTeam;
// DRow["QuestTeam"] = GuestTeam;
// DRow["loseBall"] = LoseBall;
// DRow["Time"] = MatchDateTime.ToString();
// DRow["Color"] = Color;
// dtTime = MatchDateTime;
//table.Rows.Add(DRow);
//}
// }
//}
return table;
}