/// <summary> /// 根据相传入的数据,得到相应页面数据 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> public HttpResult GetHtml(HttpItem item) { //返回参数 HttpResult result = new HttpResult(); try { //准备参数 SetRequest(item); } catch (Exception ex) { result.Cookie = string.Empty; result.Header = null; result.Html = ex.Message; result.StatusDescription = "配置参数时出错:" + ex.Message; //配置参数时出错 return result; } try { //请求数据 using (response = (HttpWebResponse)request.GetResponse()) { GetData(item, result); } } catch (WebException ex) { if (ex.Response != null) { using (response = (HttpWebResponse)ex.Response) { GetData(item, result); } } else { result.Html = ex.Message; } } catch (Exception ex) { result.Html = ex.Message; } if (item.IsToLower) result.Html = result.Html.ToLower(); return result; }
/// <summary> /// 设置编码 /// </summary> /// <param name="item">HttpItem</param> /// <param name="result">HttpResult</param> /// <param name="ResponseByte">byte[]</param> private void SetEncoding(HttpItem item, HttpResult result, byte[] ResponseByte) { //是否返回Byte类型数据 if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte; //从这里开始我们要无视编码了 if (encoding == null) { Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta[^<]*charset=([^<]*)[\"']", RegexOptions.IgnoreCase); string c = string.Empty; if (meta != null && meta.Groups.Count > 0) { c = meta.Groups[1].Value.ToLower().Trim(); } if (c.Length > 2) { try { encoding = Encoding.GetEncoding(c.Replace("\"", string.Empty).Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim()); } catch { if (string.IsNullOrEmpty(response.CharacterSet)) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } else { if (string.IsNullOrEmpty(response.CharacterSet)) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } }
/// <summary> /// 获取数据的并解析的方法 /// </summary> /// <param name="item"></param> /// <param name="result"></param> private void GetData(HttpItem item, HttpResult result) { #region base //获取StatusCode result.StatusCode = response.StatusCode; //获取StatusDescription result.StatusDescription = response.StatusDescription; //获取Headers result.Header = response.Headers; //获取CookieCollection if (response.Cookies != null) result.CookieCollection = response.Cookies; //获取set-cookie if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"]; #endregion #region byte //处理网页Byte byte[] ResponseByte = GetByte(); #endregion #region Html if (ResponseByte != null & ResponseByte.Length > 0) { //设置编码 SetEncoding(item, result, ResponseByte); //得到返回的HTML result.Html = encoding.GetString(ResponseByte); } else { //没有返回任何Html代码 result.Html = string.Empty; } #endregion }
/********************************************************************************************************** //京东模拟登录步骤 //第一步:获取登录页面基础输入值; //第二步:判断是否需要验证码,如需要则取验证码图片到本地并由用户输入; //第三步:拼装所有提交信息; //第四步:提交数据到指定服务; //第五步:获取返回信息,通过正则表达式来取需要的数据。 //https://passport.jd.com/new/login.aspx //post https://passport.jd.com/uc/loginService?uuid=b9155d01-fbe3-4a61-b15f-0fd99ea101a8&&r=0.8045588354580104&version=2015 //如uuid、r、version //uuid ; r 随机数种子 会过期,过期后提示“authcode为空”;version 京东登录脚本的版本号 //验证码图片获取地址 //https://authcode.jd.com/verify/image?a=1&acid=dd73def5-a635-4692-af7d-464491d99579&uid=dd73def5-a635-4692-af7d-464491d99579 //<div id="o-authcode" class="item item-vcode item-fore4 hide "> //https://passport.jd.com/uc/showAuthCode?r=0.7007493122946471&version=2015 **********************************************************************************************************/ /// <summary> /// 登录京东商城 /// </summary> private bool Login(string authcode) { HttpItem item = new HttpItem(); SFHttpHelper helper = new SFHttpHelper(); HttpResult result = new HttpResult(); //如果需要验证码就需要jda,jdb,jdc,jdv这些,如果没有出验证码,可以直接post登录成功的 //string cookies = "__jda=95931165.290243407.1371634814.1371634814.1371634814.1; __jdb=95931165.1.290243407|1.1371634814; __jdc=95931165; __jdv=95931165|direct|-|none|-;" + _jdLoginer.cookies; string cookies = "__jdu=1394616361; __jda=122270672.1394616361.1461636833.1461636833.1461636833.1; " +"unpl=V2_ZzNtbRBWQxYiDhMAckpaBGJRE1tLB0oSIV8UB3tNWAZjChpeclRCFXIUR1FnGlsUZwIZXUZcQBRFCHZXchBYAGEHG1hyV0YdPHhGVXoYXQRmABRdcmdAFEUAdlR5EVkCZwQQWkJncxJFOJLoxM7du7KOg4nZ9HMXdABEXH0RXANXAiJcch" +"wtFDgIRFx%2bHlwCZQQSbUM%3d; mt_subsite=||1111%2C1461636920; __jdb=122270672.5.1394616361|1.1461636833; __jdc=122270672; __jdv=122270672|click.union.timesdata.net" +"|t_288547584_149xA1000000271|tuiguang|c012f7de8b704c078a86efcb1e525892; _tp=gvgIRVyymbK6lFmmkN3g4qGeJnHoph%2BdcBXbCOaBySY%3D; unick=%E4%B8%96%E7%BA%AA%E9%AB%98%E6%A1%A5;" +" _pst=jd_7bb8087728c44; TrackID=1F5uT6zOHbmBW01TKlUyQ7GbEINQ09CbCTnXNkT2aqA8pwDg2ZR_B_Z5jWfneDJfNRQPoMXHH9sGbM7iJlmjiDX9BnAqcsEDlmDkvSQOQIH0; pinId=jc-OZwkDIvyjW1nEe9zdD7V9-x-f3wj7;" +" _jrda=1; _jrdb=1461637460015; 3AB9D23F7A4B3C9B=78d97a3d1e9e4ae2832d21dbe81d6fd7394012304; alc=/Y1Y1CquLQZPWbDaIhh/8w==; _ntvsWYk=rsy+/seWbN6G+IYtHAWOUz3B8xVwiF4oDNcJUecODec=;" +" mp=13908052076; _ntEhbrP=KPnWKr0GkGLYESVBeRIc7ucPudXAa+ADs6Z9GI6c5ug=; _ntevPgB=BAeWuzhoS+8o0U6OEKkSQH0hOa/4bp32f3dvImaQbzU=; _ntRblQD=vEujL7I++TMt+jeraYPTW/s56Fm8gBpoC9gN2FYO0XA=" + _jdLoginer.cookies; cookies = cookies.Replace("HttpOnly,", null); _jdLoginer.cookies = cookies; //https://passport.jd.com/uc/loginService?uuid=c4ee6786-a737-43f2-9110-0aea68500665&ReturnUrl=http%3A%2F%2Fwww.jd.com%2F&r=0.12566684937051964&version=2015 //https://passport.jd.com/uc/loginService?uuid=c8422a2d-e011-4783-8bca-38625607a086<ype=logout&r=0.04991701628602441&version=2015 item.URL = string.Format("https://passport.jd.com/uc/loginService?uuid={0}<ype=logout&r={1}&version=2015", _jdLoginer.uuid, _jdLoginer.r); item.Method = "post"; item.Allowautoredirect = true; item.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; item.Postdata = string.Format("uuid={0}&machineNet=&machineCpu=&machineDisk=&eid={1}&fp={2}&_t={3}&{4}={5}" + "&loginname={6}&nloginpwd={7}&loginpwd={7}&chkRememberMe=on&authcode={8}", _jdLoginer.uuid, _jdLoginer.eid, _jdLoginer.fp, _jdLoginer._t, _jdLoginer.tname, _jdLoginer.tvalue, _jdLoginer.loginname, _jdLoginer.loginpwd, authcode); item.Header.Add("x-requested-with", "XMLHttpRequest"); item.Header.Add("Accept-Encoding", "gzip, deflate"); //item.Referer = "http://passport.jd.com/new/login.aspx?ReturnUrl=http%3a%2f%2fjd2008.jd.com%2fJdHome%2fOrderList.aspx"; item.Accept = "*/*"; item.Encoding = Encoding.UTF8; item.Cookie = cookies; result = helper.GetHtml(item); _jdLoginer.cookies = result.Cookie; if (!result.Html.Contains("success")) { string rtnMsg = result.Html.Remove(0, 1).TrimEnd(')'); LoginMsg jdMsg = JsonConvert.DeserializeObject<LoginMsg>(rtnMsg); //用户名错误({"username":"******"}) if (result.Html.ToLower().Contains("username")) { ShowGetMessage("失败:用户名错误!" + jdMsg.value); ImportThreads.LastMsg = "失败:用户名错误!" + jdMsg.value; } else if (result.Html.ToLower().Contains("pwd")) { ShowGetMessage("失败:密码验证不通过!" + jdMsg.value); //密码错误 ({"pwd":"\u8d26\u6237\u540d\u4e0e\u5bc6\u7801\u4e0d\u5339\u914d\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165"}) ImportThreads.LastMsg = "失败:密码验证不通过!" + jdMsg.value; } else if (result.Html.ToLower().Contains("emptyauthcode")) { ShowGetMessage("失败:请输入登录验证码!" + jdMsg.value); //验证码错误 ({"emptyAuthcode":"\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801"}) //({"_t":"_ntcKIiJ","emptyAuthcode":"\u9a8c\u8bc1\u7801\u4e0d\u6b63\u786e\u6216\u9a8c\u8bc1\u7801\u5df2\u8fc7\u671f"}) ImportThreads.LastMsg = "失败:请输入登录验证码!" + jdMsg.value; } else { ImportThreads.LastMsg = jdMsg.value; } ImportThreads.WareEnd = true; return false; } ShowGetMessage("登录成功!"); ImportThreads.LastMsg = "登录成功!"; return true; }
/// <summary> /// 退出京东 /// </summary> private void Logout() { //退出登录 https://passport.jd.com/uc/login?ltype=logout HttpItem item = new HttpItem(); SFHttpHelper helper = new SFHttpHelper(); HttpResult result = new HttpResult(); item.URL = "https://passport.jd.com/uc/login?ltype=logout"; item.Cookie = _jdLoginer.cookies; result = helper.GetHtml(item); if (result != null) { } _jdLoginer = null; ShowGetMessage("退出京东登录"); }
/// <summary> /// 是否要验证码 /// </summary> private bool CheckAuthcode() { //判断是否需要验证 返回Json({"verifycode":false}) //https://passport.jd.com/uc/showAuthCode?r=0.7007493122946471&version=2015 //https://authcode.jd.com/verify/image?a=1&acid=1c55bd67-241f-4b29-a56b-5c5bc6c717f7&uid=1c55bd67-241f-4b29-a56b-5c5bc6c717f7&yys=1454509280755 HttpItem item = new HttpItem(); SFHttpHelper helper = new SFHttpHelper(); HttpResult result = new HttpResult(); string r = new Random().NextDouble().ToString(); item.URL = string.Format("https://passport.jd.com/uc/showAuthCode?r={0}&version=2015", r); item.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; item.Postdata = string.Format("loginName={0}",_jdLoginer.loginname); item.PostDataType = PostDataType.String; item.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"; item.Cookie = _jdLoginer.cookies; result = helper.GetHtml(item); if (result.Html.ToLower().Contains("false")) { _isAuthcode = false; return false; } else { _isAuthcode = true; return true; } }
/// <summary> /// 获取京东页面数据 /// </summary> /// <param name="website"></param> /// <returns></returns> private string GetJDWebHtml(string website) { try { if (string.IsNullOrEmpty(website)) { return null; } HttpItem item = new HttpItem(); SFHttpHelper helper = new SFHttpHelper(); HttpResult result = new HttpResult(); item.URL = website; item.Encoding = Encoding.UTF8; item.Header.Add("Accept-Encoding", "gzip, deflate"); item.ContentType = "text/html"; item.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; item.Cookie = _jdLoginer.cookies; result = helper.GetHtml(item); if (result.Cookie != null) { _jdLoginer.cookies = result.Cookie; } return result.Html; } catch (Exception ex) { ImportThreads.LastMsg = "失败:未获取到网页数据"; Debug.WriteLine(ex.Message); return null; } }