/// <summary>
/// 根据Url地址得到网页的html源码
/// </summary>
/// <param name="Url">URL地址</param>
/// <returns></returns>
public static string GetWebContent(string Url)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
catch
{
;
}
return strResult;
}
/// <summary>
/// 获取页面内容
/// </summary>
/// <param name="url">URL地址</param>
/// <param name="temp1">内容开始段</param>
/// <param name="temp2">内容结束段</param>
/// <returns></returns>
public static string gethtml(string url, string temp1, string temp2)
{
string temp = null;
try
{
//获取内容
string strWebContent = GetWebContent(url);
strWebContent = strWebContent.Replace("<", "<");
strWebContent = strWebContent.Replace(">", ">");
//取出和数据有关的那段源码
int startstr, stopstr;
startstr = strWebContent.IndexOf(temp1, 0, strWebContent.Length) + temp1.Length;
stopstr = strWebContent.IndexOf(temp2, startstr);
temp = strWebContent.Substring(startstr, stopstr - startstr);
}
catch
{
}
return temp;
}