#region 读取网页源码并保存+Cookie
/// <summary>
/// 读取网页源码并保存
/// </summary>
/// <param name="strUrl">被读页面的网址</param>
///<param name="strCookieName">Cookie名</param>
///<param name="strCookieValue">Cookie值</param>
/// <returns>网页源码字符串,如异常返回"-1"</returns>
public static string ReadWebPage2(string strUrl,string strCookieName,string strCookieValue)
{
HttpWebRequest myRq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
HttpWebResponse myRp = null;
myRq.AllowAutoRedirect = true;
myRq.Timeout = 1000*60*3;
myRq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myRq.Referer=strUrl;
CookieContainer cookie = new CookieContainer();
cookie.Add(new Uri(strUrl),new Cookie(strCookieName,strCookieValue));
myRq.CookieContainer = cookie;
StreamReader sr = null;
Stream mystr = null;
StringBuilder Html = new StringBuilder();
try
{
myRp = (HttpWebResponse)myRq.GetResponse();
mystr = myRp.GetResponseStream();
sr = new StreamReader(mystr,Encoding.Default);
while(sr.Peek()!=-1)
{
Html.Append(sr.ReadToEnd());
}
}
catch
{
return "-1";
}
finally
{
sr.Close();
mystr.Close();
myRp.Close();
}
return Html.ToString();
}
#endregion
#region 正则替换
/// <summary>
/// 正则替换
/// </summary>
/// <param name="strWebPageHtml">需要替换的源码</param>
/// <param name="strRegex">替换正则</param>
/// <returns>被替换的的代码</returns>
public static string RegexTihuan(string strWebPageHtml,string strRegex,string strNew)
{
strWebPageHtml=Regex.Replace(strWebPageHtml,strRegex,strNew);
return strWebPageHtml;
}
#endregion
#region 正则提取有效数据
/// <summary>
/// 正则提取有效数据
/// </summary>
/// <param name="strWebPageHtml">网页Html源码</param>
/// <param name="strRegex">正则表达式</param>
private void RegexCheckString(string strHtmlTemp)
{
Regex r=new Regex(this.strTiqv);
MatchCollection mc=r.Matches(strHtmlTemp);
string[] strYouxiao=new string[mc.Count];
//得到有效数据
for (int i = 0; i < mc.Count; i++)
{
string strWeb=mc[i].Value;
//无效数据过滤
strYouxiao[i]=publicClass.CstaticPublicClass.stringCheck(strWeb,this.strGuolv);
string[] strTemp=strYouxiao[i].Split(',');
//入库
InsertDatabase(strTemp);
}
}
#endregion