最近一直忙于学习抓包 一直没有更新博客 要抓取别人的得数据 首先 要进入他们的网站 所以我就用客户端模拟浏览器登录 那么就要用到htmlagliitypack这个库了 可以在vs里面直接安装 you最新的版本
private CNNWebClient webClient;
HtmlDocument html = new HtmlDocument();
string loginHtml = webClient.DownloadString("xxxxxx");//登录的网址
html.LoadHtml(loginHtml);
//判断登录是否有效
HtmlNode success = html.DocumentNode.SelectSingleNode("//*[@id=\"wrapper\"]/div[1]/div[2]/div/div[2]/a[2]");
//若登录还有效直接返回登录成功
if (success != null)
return new HttpMessage() { Reslut = success.InnerText };
//*[@id="fm1"]/div[4]/input[1]
string lt = html.DocumentNode.SelectSingleNode("//*[@id=\"fm1\"]/div[4]/input[1]").Attributes["value"].Value;//htmlagliitypack的属性获取网页上的值
string execution = html.DocumentNode.SelectSingleNode("//*[@id=\"fm1\"]/div[4]/input[2]").Attributes["value"].Value;
string postData = string.Format("username={2}&password={3}&remember=1<={0}&execution={1}&_eventId=submit"
, lt, execution, UserName, HttpUtility.UrlDecode(PassWord));//格式化字符串 用于提交
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//post提交必须加这个
webClient.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
string postHtml = webClient.UploadString("xxxxxxxxxx", postData);//登录
webClient.Headers.Add("Content-Type", "text/html;charset=UTF-8");
HtmlDocument index = new HtmlDocument();
string indexRes = webClient.DownloadString("http://eip.chinatowercom.cn/");
index.LoadHtml(indexRes);//加载首页判断是否登录成功
success = index.DocumentNode.SelectSingleNode("//*[@id=\"wrapper\"]/div[1]/div[2]/div/div[2]/a[2]");
if (success == null)
return new HttpMessage() { Reslut = postHtml, State = false };
else
{
// _addMeaage(string.Format("{0}--登录成功--", success.InnerText));
return new HttpMessage() { Reslut = success.InnerText };
}
这是登录的方法 还有客户端登录 保持cookie或者session 这个很重要
public class CNNWebClient : WebClient
{
private Calculagraph _timer;
private int _timeOut = 10;
/**/
/// <summary>
/// 返回带有 Cookie 的 HttpWebRequest。
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
HttpWebRequest httpRequest = request as HttpWebRequest;
httpRequest.CookieContainer = cookieContainer;
httpRequest.Timeout = 1000 * Timeout;
httpRequest.ReadWriteTimeout = 1000 * Timeout;
}
return request;
}
/// <summary>
/// 过期时间
/// </summary>
public int Timeout
{
get
{
return _timeOut;
}
set
{
if (value <= 0)
_timeOut = 10;
_timeOut = value;
}
}
}gan
这就是基本的登录方法了 希望能给大家一点灵感 对于程序员来说 有时候 一点灵感 足以解决困扰几天的问题