public void ParseCookie() { HttpHeader header = new HttpHeader(); HTMLHelper html = new HTMLHelper(); string url = "http://m.fang.com/my/?c=mycenter&a=index&city=bj"; url = "https://m.fang.com/passport/login.aspx"; //url = "http://m.fang.com"; //url = "http://m.fang.com/my/"; //header.method = "get"; //header.contentType = "text/html"; //var cookie = HTMLHelper.GetCookie(url,string.Empty,header); header = new HttpHeader(); header.accept = "text/html, application/xhtml+xml, */*"; header.contentType = "application/x-www-form-urlencoded; charset=GBK"; header.method = "POST"; //header.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0"; header.maxTry = 3; string postParam = "username=17701000011&password=451426&backurl=http://m.fang.com/"; var uri = new Uri(url); var cc = HTMLHelper.GetCookie(url, postParam, header, "gbk"); var cookie = cc.GetCookies(uri); var list = HTMLHelper.GetAllCookies(cc).FirstOrDefault(); var hcontent = HTMLHelper.GetHtml("http://m.fang.com/my/?c=mycenter&a=index&city=bj", cc, header, "gbk"); }
/// <summary> /// 登录:https://passport.fang.com/ /// </summary> public void LoginFangtianxiaTest() { Console.WriteLine("登录房天下测试!"); header = new HttpHeader(); header.accept = "*/*"; header.contentType = "application/x-www-form-urlencoded; charset=UTF-8"; header.method = "POST"; header.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0"; header.maxTry = 3; // 后续地址 string getUrl = "http://my.fang.com/"; string loginUrl = "https://passport.fang.com/"; string postParam = "biz_id=747420150428100001&call_time=2015-11-27 02:40:23.788&charset=GB2312&extra_param=商家ID|支付类型|backurl跳转的地址(由用户传入)&invoker=¬ify_url=&origin=EOP后台&out_trade_no=&paid_amount=0.01&platform=PC&price=0.01&quantity=1&return_url=&service=cashier_order_create_for_web&sign=&sign_type=MD5&subject=EOP商家 + 商家ID + 质保金/预存款 充值xxx元&title=EOP商家质保金&trade_amount=0.01&trade_type=30003&user_id=73318605&version=1.0&sign=0074e9c00b81e48ddf9272f85bd27257&txtusername=17701000011&password=451426&backurl=http://my.fang.com/"; CookieContainer cookieContainer = HTMLHelper.GetCookie(loginUrl, postParam, header); string html = HTMLHelper.GetHtml(getUrl, cookieContainer, header); Console.WriteLine(html); Console.ReadLine(); }
/// <summary> /// 访问博客园 /// </summary> public void ParseCnBlogCookie() { HttpHeader header = new HttpHeader(); HTMLHelper html = new HTMLHelper(); string url = string.Empty; url = "http://passport.cnblogs.com/user/signin"; header = new HttpHeader(); header.accept = "text/html, application/xhtml+xml, */*"; header.contentType = "application/x-www-form-urlencoded; charset=UTF-8"; header.method = "POST"; //header.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0"; header.maxTry = 3; string postParam = "username=*****&password=*******&backurl=http://home.cnblogs.com/u/allenstar/"; var uri = new Uri(url); var cc = HTMLHelper.GetCookie(url, postParam, header); var cookie = cc.GetCookies(uri); var list = HTMLHelper.GetAllCookies(cc); var hcontent = HTMLHelper.GetHtml("http://home.cnblogs.com/u/allenstar/", cc, header); }
/// <summary> /// 获取post之后的CooKie /// </summary> /// <param name="loginUrl"></param> /// <param name="postdata"></param> /// <param name="header"></param> /// <returns></returns> public static CookieContainer GetCookie(string loginUrl, string postdata, HttpHeader header, string encoding = "UTF-8") { HttpWebRequest request = null; HttpWebResponse response = null; try { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; CookieContainer cc = new CookieContainer(); request = (HttpWebRequest)WebRequest.Create(loginUrl); request.Method = header.method; request.ContentType = header.contentType; byte[] postdatabyte = Encoding.GetEncoding(encoding).GetBytes(postdata); request.ContentLength = postdatabyte.Length; request.AllowAutoRedirect = false; request.CookieContainer = cc; request.KeepAlive = true; //提交请求 Stream stream; stream = request.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); //接收响应 response = (HttpWebResponse)request.GetResponse(); response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); CookieCollection cook = response.Cookies; //Cookie字符串格式 string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri); return cc; } catch (Exception ex) { throw ex; } }
public void LoginRenRenTest() { header = new HttpHeader(); header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*"; header.contentType = "application/x-www-form-urlencoded"; header.method = "POST"; header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; header.maxTry = 3; // 后续地址 string getUrl = "http://guide.renren.com/guide"; //登录地址 string loginUrl = "http://www.renren.com/PLogin.do"; //请求参数 string postParam = "[email protected]&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476"; CookieContainer cookieContainer = HTMLHelper.GetCookie(loginUrl, postParam, header); string html = HTMLHelper.GetHtml(getUrl, cookieContainer, header); Console.WriteLine(html); Console.ReadLine(); }
/// <summary> /// 获取html /// </summary> /// <param name="getUrl"></param> /// <param name="cookieContainer"></param> /// <param name="header"></param> /// <returns></returns> public static string GetHtml(string getUrl, CookieContainer cookieContainer, HttpHeader header, string encoding = "UTF-8") { Thread.Sleep(1000); HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl); httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentType = header.contentType; httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry; httpWebRequest.Referer = getUrl; httpWebRequest.Accept = header.accept; httpWebRequest.UserAgent = header.userAgent; httpWebRequest.Method = "GET"; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding)); string html = streamReader.ReadToEnd(); streamReader.Close(); responseStream.Close(); httpWebRequest.Abort(); httpWebResponse.Close(); return html; } catch (Exception e) { if (httpWebRequest != null) httpWebRequest.Abort(); if (httpWebResponse != null) httpWebResponse.Close(); return string.Empty; } }