/// <summary> /// 获取AccessToken /// </summary> private void GetAccessToken() { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = string.Format("{0}token?grant_type=client_credential&appid={1}&secret={2}", config.APIUrl, config.AppID, config.AppSecret); string json = http.GetHtml(item); var data = new WeChatData(json); config.AccessToken = data["access_token"]; config.ExpiresIn = DateTime.Now.AddSeconds(data.Get<int>("expires_in")); }
/// <summary> /// 根据相传入的数据,得到相应页面数据 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> internal HttpResult GetHtml(HttpItem item) { if (item.Allowautoredirect && item.AutoRedirectCookie) { HttpResult result = null; for (int i = 0; i < 100; i++) { item.Allowautoredirect = false; result = httpbase.GetHtml(item); if (string.IsNullOrWhiteSpace(result.RedirectUrl)) { break; } item.URL = result.RedirectUrl; item.Method = "GET"; } return result; } return httpbase.GetHtml(item); }
/// <summary> /// 实时天气 /// </summary> /// <returns></returns> public static string GetSK(string id, string name = null, string latitude = null, string longitude = null) { ComLib.LogLib.Log4NetBase.Log("调用GeSK-时间:" + DateTime.Now.ToString()); var ID = id ?? Data.GetID(name ?? BaiduMap.GetCity(latitude, longitude)); if (ID.IsNullOrEmpty()) { return "{\"status\":\"201\",\"message\":\"城市代码获取失败\"}"; } string url = string.Format("http://www.weather.com.cn/data/sk/{0}.html", ID); HttpHelper request = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = url; var jsonhtml = request.GetHtml(item).Html; if (Regex.IsMatch(jsonhtml, "404") || Regex.IsMatch(jsonhtml, "302")) { return "{\"status\":\"201\",\"message\":\"数据获取失败\"}"; } return jsonhtml; }
/// <summary> /// 快速Post数据这个访求与GetHtml一样,只是不接收返回数据,只做提交。 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> internal HttpResult FastRequest(HttpItem item) { //返回参数 HttpResult result = new HttpResult(); try { //准备参数 SetRequest(item); } catch (Exception ex) { //配置参数时出错 return new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message }; } try { //请求数据 using (response = (HttpWebResponse)request.GetResponse()) { //成功 不做处理只回成功状态 return new HttpResult() { Cookie = string.Empty, Header = response.Headers, StatusCode = response.StatusCode, StatusDescription = response.StatusDescription }; } } catch (WebException ex) { using (response = (HttpWebResponse)ex.Response) { //不做处理只回成功状态 return new HttpResult() { Cookie = string.Empty, Header = response.Headers, StatusCode = response.StatusCode, StatusDescription = response.StatusDescription }; } } catch (Exception ex) { result.Html = ex.Message; } if (item.IsToLower) result.Html = result.Html.ToLower(); return result; }
/// <summary> /// 为请求准备参数 /// </summary> ///<param name="item">参数列表</param> private void SetRequest(HttpItem item) { if (!string.IsNullOrWhiteSpace(item.CerPath)) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); } //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(item.URL); if (item.IPEndPoint != null) { _IPEndPoint = item.IPEndPoint; //设置本地的出口ip和端口 request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback); } request.AutomaticDecompression = item.AutomaticDecompression; // 验证证书 SetCer(item); SetCerList(item); //设置Header参数 if (item.Header != null && item.Header.Count > 0) { foreach (string key in item.Header.AllKeys) { request.Headers.Add(key, item.Header[key]); } } // 设置代理 SetProxy(item); if (item.ProtocolVersion != null) { request.ProtocolVersion = item.ProtocolVersion; } request.ServicePoint.Expect100Continue = item.Expect100Continue; //请求方式Get或者Post request.Method = item.Method; request.Timeout = item.Timeout; request.KeepAlive = item.KeepAlive; request.ReadWriteTimeout = item.ReadWriteTimeout; if (!string.IsNullOrWhiteSpace(item.Host)) { request.Host = item.Host; } if (item.IfModifiedSince != null) { request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince); } //Accept request.Accept = item.Accept; //ContentType返回类型 request.ContentType = item.ContentType; //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息 request.UserAgent = item.UserAgent; // 编码 encoding = item.Encoding; //设置安全凭证 request.Credentials = item.ICredentials; //设置Cookie SetCookie(item); //来源地址 request.Referer = item.Referer; //是否执行跳转功能 request.AllowAutoRedirect = item.Allowautoredirect; if (item.MaximumAutomaticRedirections > 0) { request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections; } //设置最大连接 if (item.Connectionlimit > 0) { request.ServicePoint.ConnectionLimit = item.Connectionlimit; } //当出现“请求被中止: 未能创建 SSL/TLS 安全通道”时需要配置此属性 if (item.SecurityProtocol > 0) { ServicePointManager.SecurityProtocol = item.SecurityProtocol; } //设置Post数据 SetPostData(item); }
/// <summary> /// 设置证书 /// </summary> /// <param name="item"></param> private void SetCer(HttpItem item) { if (!string.IsNullOrWhiteSpace(item.CerPath)) { //将证书添加到请求里 if (!string.IsNullOrWhiteSpace(item.CerPwd)) { request.ClientCertificates.Add(new X509Certificate(item.CerPath, item.CerPwd)); } else { request.ClientCertificates.Add(new X509Certificate(item.CerPath)); } } }
/// <summary> /// 设置代理 /// </summary> /// <param name="item">参数对象</param> private void SetProxy(HttpItem item) { bool isIeProxy = false; if (!string.IsNullOrWhiteSpace(item.ProxyIp)) { isIeProxy = item.ProxyIp.ToLower().Contains("ieproxy"); } if (!string.IsNullOrWhiteSpace(item.ProxyIp) && !isIeProxy) { //设置代理服务器 if (item.ProxyIp.Contains(":")) { string[] plist = item.ProxyIp.Split(':'); WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim())); //建议连接 myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd); //给当前请求对象 request.Proxy = myProxy; } else { WebProxy myProxy = new WebProxy(item.ProxyIp, false); //建议连接 myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd); //给当前请求对象 request.Proxy = myProxy; } } else if (isIeProxy) { //设置为IE代理 } else { // request.Proxy = item.WebProxy; } }
/// <summary> /// 发送手机短信 /// </summary> /// <param name="mobile">手机号码,以英文“,”逗号分隔开</param> /// <param name="content">短信内容</param> /// <param name="pass">无作用(备用)</param> /// <param name="msg">返回提示信息</param> /// <returns>bool</returns> public bool Send(string mobile, string content, int pass, out string msg) { if (string.IsNullOrEmpty(this.username) || string.IsNullOrEmpty(this.password) || string.IsNullOrEmpty(apicode)) { msg = "短信配置参数有误,请完善后再提交!"; return(false); } //验证手机号码 Regex r = new Regex(@"^1(3|4|5|7|8)\d{9}$", RegexOptions.IgnoreCase); if (!r.Match(mobile).Success) { msg = "手机号码格式不正确!"; return(false); } bool status = true; BLL.sms_log bll = new BLL.sms_log(); //查询是否超出平台限制 int thisSafeTotal = bll.GetCurDayCount(); if (this.safeTotal > 0 && thisSafeTotal > this.safeTotal) { msg = "对不起,平台短信发送量已超出最大限制!"; status = false; } //查询当前IP是否已经超出限制 string ip = DTRequest.GetIP(); int thisIpSendCount = bll.GetIPCount(ip); if (this.ipCount > 0 && thisIpSendCount > this.ipCount) { msg = "对不起,你的网络已经超出发送数量限制!"; status = false; } msg = string.Empty; if (status) { //发送短信 Model.sms_log model = new Model.sms_log(); model.mobile = mobile; model.content = content; model.send_time = DateTime.Now; try { StringBuilder parameter = new StringBuilder(); parameter.AppendFormat("SpCode={0}&LoginName={1}&Password={2}&MessageContent={3}", this.apicode, this.username, this.password, content); parameter.AppendFormat("&UserNumber={0}&SerialNumber={1}&ScheduleTime=&ExtendAccessNum=&f=1", mobile, DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001"); byte[] data = Encoding.GetEncoding("GBK").GetBytes(parameter.ToString()); HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://sms.api.ums86.com:8899/sms/Api/Send.do", Encoding = Encoding.Default, Method = "POST", ContentType = "application/x-www-form-urlencoded", PostdataByte = data }; HttpResult result = http.GetHtml(item); //获取返回值 string code = QueryParameter(result.Html, "result"); //获取描述 string desc = QueryParameter(result.Html, "description"); if (string.IsNullOrEmpty(desc)) { //加载错误表 Dictionary <string, string> dic = ErrorDic(); if (dic.ContainsKey(code)) { desc = dic[code]; } } if (code == "0") { model.status = 0; model.remark = code; } else { model.status = 1; model.remark = desc; } } catch (Exception ex) { status = false; model.status = 1; model.remark = "提交失败,错误提示:" + ex.Message; msg = model.remark; } bll.Add(model); } //返回状态 if (status) { msg = "发送成功!"; return(true); } return(false); }
/********************************************************************************************************** * //京东模拟登录步骤 * //第一步:获取登录页面基础输入值; * //第二步:判断是否需要验证码,如需要则取验证码图片到本地并由用户输入; * //第三步:拼装所有提交信息; * //第四步:提交数据到指定服务; * //第五步:获取返回信息,通过正则表达式来取需要的数据。 * //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> /// 根据Url获取图片 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回图片</returns> internal Image GetImage(HttpItem item) { item.ResultType = ResultType.Byte; return ImageHelper.ByteToImage(GetHtml(item).ResultByte); }
private string GetResponseData(string url, string requestStr, string cookies) { //创建Httphelper对象 HttpHelper http = new HttpHelper(); //创建Httphelper参数对象 var item = new HttpItem() { URL = url, //URL,必需项 Method = this.cbRequestType.Text, Encoding = Encoding.UTF8, Cookie = cookies }; //其中None和QueryString都是通过URL直接访问的,不需要增加其他参数 if (this.rbForm.IsChecked == true) { var lines = requestStr.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var line in lines) { var index = line.IndexOf(':'); if (index > 0 && index < line.Length - 1) { var str1 = line.Substring(0, index).Trim(); var str2 = line.Substring(index + 1).Trim(); if (!string.IsNullOrEmpty(str1)) { sb.Append($"{str1}={str2}&"); } } } var requestData = sb.ToString().TrimEnd('&'); item.PostDataType = CsharpHttpHelper.Enum.PostDataType.String; item.Postdata = requestData; item.ContentType = "application/x-www-form-urlencoded"; } else if (this.rbJson.IsChecked == true) { item.PostDataType = CsharpHttpHelper.Enum.PostDataType.String; if (!requestStr.StartsWith("{")) { var lines = requestStr.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var line in lines) { var index = line.IndexOf(':'); if (index > 0 && index < line.Length - 1) { var str1 = line.Substring(0, index).Trim(); var str2 = line.Substring(index + 1).Trim(); if (!string.IsNullOrEmpty(str1)) { sb.Append($"\"{str1}\":{str2},"); } } } item.Postdata = "{" + sb.ToString().TrimEnd(',') + "}"; } else { item.Postdata = requestStr.Replace("\r\n", " "); } item.ContentType = "application/json"; } //请求的返回值对象 HttpResult result = http.GetHtml(item); //获取当前页数据 return(result.Html); }
/// <summary> /// 设置Cookie /// </summary> /// <param name="item">Http参数</param> private void SetCookie(HttpItem item) { //Cookie if (item.ResultCookieType == ResultCookieType.String) { if (!string.IsNullOrEmpty(item.Cookie)) request.Headers[HttpRequestHeader.Cookie] = item.Cookie; } //设置CookieCollection else if (item.ResultCookieType == ResultCookieType.CookieCollection) { request.CookieContainer = new CookieContainer(); if (item.CookieCollection != null && item.CookieCollection.Count > 0) request.CookieContainer.Add(item.CookieCollection); } }
public string Excute(WeChatConfig config, WeChatData data) { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = string.Format("{0}menu/delete?access_token={1}", config.APIUrl, config.AccessToken); item.Method = "GET"; string result = http.GetHtml(item); return result; }
/// <summary> /// 模拟登陆,ip代理可能需要用到 /// </summary> /// <returns></returns> public bool SimulateLogin() { if (isSpecialUrlMode) { return(true); } //之前似乎否已经登陆过了 if (!string.IsNullOrEmpty(Settings.LoginAccount)) { DBChangeQueue.Instance.EnQueue(new StorageData() { Document = new BsonDocument().Add(DataTableName + "status", "1"), Name = DataTableNameAccount, Query = Query.EQ("userName", Settings.LoginAccount), Type = StorageType.Update }); StartDBChangeProcess(); } var accountBson = AccountQueue.Instance.DeQueue(); if (accountBson == null) { Console.WriteLine("账号已用完"); Environment.Exit(0); return(false); } IPProxy ipProxy = null; HttpHelper http = new HttpHelper(); //尝试登陆 while (true) { try { ipProxy = Settings.GetIPProxy(); if (ipProxy == null || string.IsNullOrEmpty(ipProxy.IP)) { Settings.SimulateCookies = string.Empty; //return true; } HttpItem item = new HttpItem() { URL = "https://passport.fang.com/login.api", //URL 必需项 Encoding = null, //编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别 //Encoding = Encoding.Default, Method = "post", //URL 可选项 默认为Get //Timeout = 100000,//连接超时时间 可选项默认为100000 //ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000 //IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写 //Cookie = "",//字符串Cookie 可选项 UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko", //用户的浏览器类型,版本,操作系统 可选项有默认值 Accept = "text/html, application/xhtml+xml, */*", // 可选项有默认值 ContentType = "application/x-www-form-urlencoded", //返回类型 可选项有默认值 Referer = "https://passport.fang.com/", //来源URL 可选项 Allowautoredirect = true, }; if (accountBson != null) { item.Postdata = accountBson.Text("postData"); } if (ipProxy != null) { item.ProxyIp = ipProxy.IP; } if (Settings.CurWebProxy != null) { item.WebProxy = Settings.CurWebProxy; } Console.WriteLine(string.Format("尝试登陆{0}", Settings.curIPProxy != null ? Settings.curIPProxy.IP : string.Empty)); HttpResult result = http.GetHtml(item); if (!result.Html.Contains("Success")) { return(false); } string cookie = string.Empty; foreach (CookieItem s in HttpCookieHelper.GetCookieList(result.Cookie)) { //if (s.Key.Contains("24a79_")) { cookie += HttpCookieHelper.CookieFormat(s.Key, s.Value); } } if (accountBson != null) { var account = accountBson.Text("userName"); if (result.Html.IndexOf("Success") > 0) { Settings.SimulateCookies = cookie;//设置cookie值 Settings.LoginAccount = account; Console.WriteLine(string.Format("{0}模拟登陆成功", account)); return(true); } DBChangeQueue.Instance.EnQueue(new StorageData() { Document = new BsonDocument().Add("status", "1"), Name = DataTableNameAccount, Query = Query.EQ("userName", account), Type = StorageType.Update }); StartDBChangeProcess(); } else { Settings.SimulateCookies = cookie;//设置cookie值 Console.WriteLine("登陆失败"); return(false); } return(false); } catch (WebException ex) { IPInvalidProcess(ipProxy); } catch (Exception ex) { IPInvalidProcess(ipProxy); } } }
/// <summary> /// 获取HTML /// </summary> /// <param name="userNameMan">男方姓名</param> /// <param name="birthDayMan">男方出生日期</param> /// <param name="timeMan">男方时辰</param> /// <param name="userNameWoman">女方姓名</param> /// <param name="birthDayWoman">女方出生日期</param> /// <param name="timeWoman">女方时辰</param> /// <returns></returns> private static string Html(string userNameMan, DateTime birthDayMan, string timeMan, string userNameWoman, DateTime birthDayWoman, string timeWoman) { //TODO VALID time //timeMan = timeWoman = "不清楚"; var yearMan = birthDayMan.Year; var monthMan = birthDayMan.Month.ToString().Length == 1 ? "0" + birthDayMan.Month : birthDayMan.Month.ToString(); var dayMan = birthDayMan.Day.ToString().Length == 1 ? "0" + birthDayMan.Day : birthDayMan.Day.ToString(); var yearWoman = birthDayWoman.Year; var monthWoman = birthDayWoman.Month.ToString().Length == 1 ? "0" + birthDayMan.Month : birthDayMan.Month.ToString(); var dayWoman = birthDayWoman.Day.ToString().Length == 1 ? "0" + birthDayMan.Day : birthDayMan.Day.ToString(); var userNameManEn = System.Web.HttpUtility.UrlEncode(userNameMan, Encoding.GetEncoding("GB2312")) ?.ToUpper(); var userNameWomanEn = System.Web.HttpUtility.UrlEncode(userNameWoman, Encoding.GetEncoding("GB2312")) ?.ToUpper(); var timeManEn = System.Web.HttpUtility.UrlEncode(timeMan, Encoding.GetEncoding("GB2312"))?.ToUpper(); var timeWomanEn = System.Web.HttpUtility.UrlEncode(timeWoman, Encoding.GetEncoding("GB2312"))?.ToUpper(); var postData = "UserName="******"&Sex=M&iYear=" + yearMan + "&iMonth=" + monthMan + "&iDay=" + dayMan + "&iHour=" + timeManEn + "&UserNameB=" + userNameWomanEn + "&SexB=F&iYearB=" + yearWoman + "&iMonthB=" + monthWoman + "&iDayB=" + dayWoman + "&iHourB=" + timeWomanEn; var html = string.Empty; try { var http = new HttpHelper(); var item = new HttpItem() { URL = Url, //URL 必需项 Method = "POST", //URL 可选项 默认为Get Postdata = postData, UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36", //用户的浏览器类型,版本,操作系统 可选项有默认值 Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", // 可选项有默认值 Referer = "http://bz.99166.com/bzhh/", ContentType = "application/x-www-form-urlencoded", //返回类型 可选项有默认值 ResultType = ResultType.String, //返回数据类型,是Byte还是String }; var result = http.GetHtml(item); html = result.Html; } catch (Exception ex) { LogService.InsertLog(ex); } return(html); }
//读取每分钟数据 private void button1_Click(object sender, EventArgs e) { List <StockInfo> stocklist = _Stockservice.GetModelList(""); string a = DateTime.Now.ToString(); int y = stocklist.Count / 100 + 1; for (int num = 0; num < y; num++) { int c = 100 + num * 100; if (c > stocklist.Count) { c = stocklist.Count; } List <string> stocklist2 = new List <string>(); HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = "http://api.money.126.net/data/feed/"; for (int stockCount = num * 100; stockCount < c; stockCount++) { string x = stocklist[stockCount].ToString(); switch (stocklist[stockCount].stockcode.Substring(0, 1)) { //3-sz //0-sz //6-sh default: MessageBox.Show("1到5之外的数"); break; case "0": item.URL += "1" + stocklist[stockCount].stockcode + ","; stocklist2.Add("1" + stocklist[stockCount].stockcode); break; case "3": item.URL += "1" + stocklist[stockCount].stockcode + ","; stocklist2.Add("1" + stocklist[stockCount].stockcode); break; case "6": item.URL += "0" + stocklist[stockCount].stockcode + ","; stocklist2.Add("0" + stocklist[stockCount].stockcode); break; case "1": item.URL += "1" + stocklist[stockCount].stockcode + ","; stocklist2.Add("1" + stocklist[stockCount].stockcode); break; case "2": item.URL += "1" + stocklist[stockCount].stockcode + ","; stocklist2.Add("1" + stocklist[stockCount].stockcode); break; } } item.URL += "money.api"; item.Encoding = Encoding.UTF8; item.Method = "GET"; item.Timeout = 100000; item.ReadWriteTimeout = 30000;//写入Post数据超时时间,可选项默认为30000 HttpResult result = http.GetHtml(item); string html = result.Html; string statusCodeDescription = result.StatusDescription; //示例: //_ntes_quote_callback({"0601398":{"code": "0601398", "percent": 0.02314, "share": "1", "high": 6.26, "askvol3": 2000, "askvol2": 117612, "askvol5": 1075200, "askvol4": 1098800, "price": 6.19, "open": 6.05, "bid5": 6.13, "bid4": 6.14, "bid3": 6.15, "bid2": 6.16, "bid1": 6.17, "low": 6.02, "updown": 0.14, "type": "SH", "symbol": "601398", "status": 0, "ask4": 6.21, "bidvol3": 759900, "bidvol2": 762600, "bidvol1": 320900, "update": "2017/10/27 15:59:49", "bidvol5": 1270500, "bidvol4": 613200, "yestclose": 6.05, "askvol1": 66800, "ask5": 6.22, "volume": 456396456, "ask1": 6.18, "name": "\u5de5\u5546\u94f6\u884c", "ask3": 6.2, "ask2": 6.19, "arrow": "\u2191", "time": "2017/10/27 15:30:00", "turnover": 2808909770} }); html = html.Replace("_ntes_quote_callback(", ""); html = html.Replace(");", ""); //object b = JsonConvert.DeserializeObject(html); JObject ja = (JObject)JsonConvert.DeserializeObject(html); JObject jb = (JObject)ja["0601398"]; JObject jb2 = (JObject)ja["1000002"]; //StockMinInfo stock2 = JsonConvert.DeserializeObject<StockMinInfo>(jb.ToString()); //StockMinInfo stock3 = JsonConvert.DeserializeObject<StockMinInfo>(ja["10000002"].ToString()); foreach (string x in stocklist2) { StockMinInfo stock4 = JsonConvert.DeserializeObject <StockMinInfo>(ja[x].ToString()); int ID = _StockMinservice.Add(stock4); if (ID > 0) { } else { MessageBox.Show(x.ToString()); } } } MessageBox.Show(a + " " + DateTime.Now.ToString()); }
private void button3_Click(object sender, EventArgs e) { List <StockInfo> stocklist = _Stockservice.GetModelList(""); foreach (StockInfo s in stocklist) { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = " http://quotes.money.163.com/service/zcfzb_" + s.stockcode + ".html"; item.Encoding = Encoding.UTF8; item.Method = "GET"; item.Timeout = 100000; item.ReadWriteTimeout = 30000;//写入Post数据超时时间,可选项默认为30000 HttpResult result = http.GetHtml(item); string Result = result.Html.Replace("\r\n\t", "").Replace(" ", ""); //string[] arrTemp = result.Html.Split('\r\n'); string[] strlist = Result.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (strlist.Length == 72 || strlist.Length == 1) { continue; } string[] ReportDate = strlist[0].Substring(0, strlist[0].Length - 1).Split(','); string[] HBZJ = strlist[1].Substring(0, strlist[1].Length - 1).Split(','); string[] JSBFJ = strlist[2].Substring(0, strlist[2].Length - 1).Split(','); string[] CCZJ = strlist[3].Substring(0, strlist[3].Length - 1).Split(','); string[] JYXJRZC = strlist[4].Substring(0, strlist[4].Length - 1).Split(','); string[] YSJRZC = strlist[5].Substring(0, strlist[5].Length - 1).Split(','); string[] YSPJ = strlist[6].Substring(0, strlist[6].Length - 1).Split(','); string[] YSZK = strlist[7].Substring(0, strlist[7].Length - 1).Split(','); string[] YFKX = strlist[8].Substring(0, strlist[8].Length - 1).Split(','); string[] YSBF = strlist[9].Substring(0, strlist[9].Length - 1).Split(','); string[] YSFBZK = strlist[10].Substring(0, strlist[10].Length - 1).Split(','); string[] YSFBHTZBJ = strlist[11].Substring(0, strlist[11].Length - 1).Split(','); string[] YSLX = strlist[12].Substring(0, strlist[12].Length - 1).Split(','); string[] YSGL = strlist[13].Substring(0, strlist[13].Length - 1).Split(','); string[] QTYSK = strlist[14].Substring(0, strlist[14].Length - 1).Split(','); string[] YSCKTS = strlist[15].Substring(0, strlist[15].Length - 1).Split(','); string[] YSBTK = strlist[16].Substring(0, strlist[16].Length - 1).Split(','); string[] YSBZJ = strlist[17].Substring(0, strlist[17].Length - 1).Split(','); string[] NBYSK = strlist[18].Substring(0, strlist[18].Length - 1).Split(','); string[] MRFSJRZC = strlist[19].Substring(0, strlist[19].Length - 1).Split(','); string[] CH = strlist[20].Substring(0, strlist[20].Length - 1).Split(','); string[] DTFY = strlist[21].Substring(0, strlist[21].Length - 1).Split(','); string[] DCLLDZCSS = strlist[22].Substring(0, strlist[22].Length - 1).Split(','); string[] YNNDQDFLDZC = strlist[23].Substring(0, strlist[23].Length - 1).Split(','); string[] QTLDZC = strlist[24].Substring(0, strlist[24].Length - 1).Split(','); string[] LDZCHJ = strlist[25].Substring(0, strlist[25].Length - 1).Split(','); string[] FCDKJDK = strlist[26].Substring(0, strlist[26].Length - 1).Split(','); string[] KGCSJRZC = strlist[27].Substring(0, strlist[27].Length - 1).Split(','); string[] CYZDQTZ = strlist[28].Substring(0, strlist[28].Length - 1).Split(','); string[] CQYSK = strlist[29].Substring(0, strlist[29].Length - 1).Split(','); string[] CQGQTZ = strlist[30].Substring(0, strlist[30].Length - 1).Split(','); string[] QTCQTZ = strlist[31].Substring(0, strlist[31].Length - 1).Split(','); string[] TZXFDC = strlist[32].Substring(0, strlist[32].Length - 1).Split(','); string[] GDZCYZ = strlist[33].Substring(0, strlist[33].Length - 1).Split(','); string[] LJZJ = strlist[34].Substring(0, strlist[34].Length - 1).Split(','); string[] GDZCJZ = strlist[35].Substring(0, strlist[35].Length - 1).Split(','); string[] GDZCJZZB = strlist[36].Substring(0, strlist[36].Length - 1).Split(','); string[] GDZC = strlist[37].Substring(0, strlist[37].Length - 1).Split(','); string[] ZJGC = strlist[38].Substring(0, strlist[38].Length - 1).Split(','); string[] GCWZ = strlist[39].Substring(0, strlist[39].Length - 1).Split(','); string[] GDZCQL = strlist[40].Substring(0, strlist[40].Length - 1).Split(','); string[] SCXSWZC = strlist[41].Substring(0, strlist[41].Length - 1).Split(','); string[] GYXSWZC = strlist[42].Substring(0, strlist[42].Length - 1).Split(','); string[] QYZC = strlist[43].Substring(0, strlist[43].Length - 1).Split(','); string[] WXZC = strlist[44].Substring(0, strlist[44].Length - 1).Split(','); string[] KFZC = strlist[45].Substring(0, strlist[45].Length - 1).Split(','); string[] SY = strlist[46].Substring(0, strlist[46].Length - 1).Split(','); string[] CQDTFY = strlist[47].Substring(0, strlist[47].Length - 1).Split(','); string[] GQFZLTQ = strlist[48].Substring(0, strlist[48].Length - 1).Split(','); string[] DYSDSZC = strlist[49].Substring(0, strlist[49].Length - 1).Split(','); string[] QTFLDZC = strlist[50].Substring(0, strlist[50].Length - 1).Split(','); string[] FLDZCHJ = strlist[51].Substring(0, strlist[51].Length - 1).Split(','); string[] ZCZJ = strlist[52].Substring(0, strlist[52].Length - 1).Split(','); string[] DQJK = strlist[53].Substring(0, strlist[53].Length - 1).Split(','); string[] XZYYHJK = strlist[54].Substring(0, strlist[54].Length - 1).Split(','); string[] XSCKJTYCF = strlist[55].Substring(0, strlist[55].Length - 1).Split(','); string[] CRZJ = strlist[56].Substring(0, strlist[56].Length - 1).Split(','); string[] JYXJRFZ = strlist[57].Substring(0, strlist[57].Length - 1).Split(','); string[] YSJRFZ = strlist[58].Substring(0, strlist[58].Length - 1).Split(','); string[] YFPJ = strlist[59].Substring(0, strlist[59].Length - 1).Split(','); string[] YFZK = strlist[60].Substring(0, strlist[60].Length - 1).Split(','); string[] YuSZK = strlist[61].Substring(0, strlist[61].Length - 1).Split(','); string[] MCHGJRZCK = strlist[62].Substring(0, strlist[62].Length - 1).Split(','); string[] YFSXFJYJ = strlist[63].Substring(0, strlist[63].Length - 1).Split(','); string[] YFZGXC = strlist[64].Substring(0, strlist[64].Length - 1).Split(','); string[] YJSF = strlist[65].Substring(0, strlist[65].Length - 1).Split(','); string[] YFLX = strlist[66].Substring(0, strlist[66].Length - 1).Split(','); string[] YFGL = strlist[67].Substring(0, strlist[67].Length - 1).Split(','); string[] QTYJK = strlist[68].Substring(0, strlist[68].Length - 1).Split(','); string[] YFBZJ = strlist[69].Substring(0, strlist[69].Length - 1).Split(','); string[] NBYFK = strlist[70].Substring(0, strlist[70].Length - 1).Split(','); string[] QTYFK = strlist[71].Substring(0, strlist[71].Length - 1).Split(','); string[] YTFY = strlist[72].Substring(0, strlist[72].Length - 1).Split(','); string[] YJLDFZ = strlist[73].Substring(0, strlist[73].Length - 1).Split(','); string[] YFFBZK = strlist[74].Substring(0, strlist[74].Length - 1).Split(','); string[] BXHTZBJ = strlist[75].Substring(0, strlist[75].Length - 1).Split(','); string[] DLMMZQK = strlist[76].Substring(0, strlist[76].Length - 1).Split(','); string[] DLCXZQK = strlist[77].Substring(0, strlist[77].Length - 1).Split(','); string[] GJPZJS = strlist[78].Substring(0, strlist[78].Length - 1).Split(','); string[] GNPZJS = strlist[79].Substring(0, strlist[79].Length - 1).Split(','); string[] DYSY = strlist[80].Substring(0, strlist[80].Length - 1).Split(','); string[] YFDQZQ = strlist[81].Substring(0, strlist[81].Length - 1).Split(','); string[] YNDDQDFLDFZ = strlist[82].Substring(0, strlist[82].Length - 1).Split(','); string[] QTLDFZ = strlist[83].Substring(0, strlist[83].Length - 1).Split(','); string[] LDFZHJ = strlist[84].Substring(0, strlist[84].Length - 1).Split(','); string[] CQJQ = strlist[85].Substring(0, strlist[85].Length - 1).Split(','); string[] YFZQ = strlist[86].Substring(0, strlist[86].Length - 1).Split(','); string[] CQYFZQ = strlist[87].Substring(0, strlist[87].Length - 1).Split(','); string[] ZXYFK = strlist[88].Substring(0, strlist[88].Length - 1).Split(','); string[] YJFLDFZ = strlist[89].Substring(0, strlist[89].Length - 1).Split(','); string[] CQDYSY = strlist[90].Substring(0, strlist[90].Length - 1).Split(','); string[] DYSDSFZ = strlist[91].Substring(0, strlist[91].Length - 1).Split(','); string[] QTFLDFZ = strlist[92].Substring(0, strlist[92].Length - 1).Split(','); string[] FLDFZHJ = strlist[93].Substring(0, strlist[93].Length - 1).Split(','); string[] FZHJ = strlist[94].Substring(0, strlist[94].Length - 1).Split(','); string[] SSZB = strlist[95].Substring(0, strlist[95].Length - 1).Split(','); string[] ZBGJ = strlist[96].Substring(0, strlist[96].Length - 1).Split(','); string[] JKCG = strlist[97].Substring(0, strlist[97].Length - 1).Split(','); string[] ZXCB = strlist[98].Substring(0, strlist[98].Length - 1).Split(','); string[] YYGJ = strlist[99].Substring(0, strlist[99].Length - 1).Split(','); string[] YBFXZB = strlist[100].Substring(0, strlist[100].Length - 1).Split(','); string[] WQDDTZSS = strlist[101].Substring(0, strlist[101].Length - 1).Split(','); string[] WFPLR = strlist[102].Substring(0, strlist[102].Length - 1).Split(','); string[] NFPXJGL = strlist[103].Substring(0, strlist[103].Length - 1).Split(','); string[] WBBBZSCE = strlist[104].Substring(0, strlist[104].Length - 1).Split(','); string[] GSYMGSGDQYHJ = strlist[105].Substring(0, strlist[105].Length - 1).Split(','); string[] SSGDQY = strlist[106].Substring(0, strlist[106].Length - 1).Split(','); string[] SYZQY = strlist[107].Substring(0, strlist[107].Length - 1).Split(','); string strlist109 = strlist[108].Replace("\t", ""); string[] FZHSYZQY = strlist109.Substring(0, strlist109.Length - 1).Split(','); for (int num = 1; num < ReportDate.Length; num++) { int IsHave = _StockZCFZService.GetRecordCount("Code='" + s.stockcode + "' AND ReportDate=CONVERT(datetime,'" + ReportDate[num].ToString() + "',102)"); if (IsHave != 0) { continue; } StockZCFZInfo model = new StockZCFZInfo(); model.Code = s.stockcode; model.ReportDate = Convert.ToDateTime(ReportDate[num].ToString()); model.HBZJ = PublicTool.IsNumElseToZero(HBZJ[num].ToString()); model.JSBFJ = PublicTool.IsNumElseToZero(JSBFJ[num].ToString()); model.CCZJ = PublicTool.IsNumElseToZero(CCZJ[num].ToString()); model.JYXJRZC = PublicTool.IsNumElseToZero(JYXJRZC[num].ToString()); model.YSJRZC = PublicTool.IsNumElseToZero(YSJRZC[num].ToString()); model.YSPJ = PublicTool.IsNumElseToZero(YSPJ[num].ToString()); model.YSZK = PublicTool.IsNumElseToZero(YSZK[num].ToString()); model.YFKX = PublicTool.IsNumElseToZero(YFKX[num].ToString()); model.YSBF = PublicTool.IsNumElseToZero(YSBF[num].ToString()); model.YSFBZK = PublicTool.IsNumElseToZero(YSFBZK[num].ToString()); model.YSFBHTZBJ = PublicTool.IsNumElseToZero(YSFBHTZBJ[num].ToString()); model.YSLX = PublicTool.IsNumElseToZero(YSLX[num].ToString()); model.YSGL = PublicTool.IsNumElseToZero(YSGL[num].ToString()); model.QTYSK = PublicTool.IsNumElseToZero(QTYSK[num].ToString()); model.YSCKTS = PublicTool.IsNumElseToZero(YSCKTS[num].ToString()); model.YSBTK = PublicTool.IsNumElseToZero(YSBTK[num].ToString()); model.YSBZJ = PublicTool.IsNumElseToZero(YSBZJ[num].ToString()); model.NBYSK = PublicTool.IsNumElseToZero(NBYSK[num].ToString()); model.MRFSJRZC = PublicTool.IsNumElseToZero(MRFSJRZC[num].ToString()); model.CH = PublicTool.IsNumElseToZero(CH[num].ToString()); model.DTFY = PublicTool.IsNumElseToZero(DTFY[num].ToString()); model.DCLLDZCSS = PublicTool.IsNumElseToZero(DCLLDZCSS[num].ToString()); model.YNNDQDFLDZC = PublicTool.IsNumElseToZero(YNNDQDFLDZC[num].ToString()); model.QTLDZC = PublicTool.IsNumElseToZero(QTLDZC[num].ToString()); model.LDZCHJ = PublicTool.IsNumElseToZero(LDZCHJ[num].ToString()); model.FCDKJDK = PublicTool.IsNumElseToZero(FCDKJDK[num].ToString()); model.KGCSJRZC = PublicTool.IsNumElseToZero(KGCSJRZC[num].ToString()); model.CYZDQTZ = PublicTool.IsNumElseToZero(CYZDQTZ[num].ToString()); model.CQYSK = PublicTool.IsNumElseToZero(CQYSK[num].ToString()); model.CQGQTZ = PublicTool.IsNumElseToZero(CQGQTZ[num].ToString()); model.QTCQTZ = PublicTool.IsNumElseToZero(QTCQTZ[num].ToString()); model.TZXFDC = PublicTool.IsNumElseToZero(TZXFDC[num].ToString()); model.GDZCYZ = PublicTool.IsNumElseToZero(GDZCYZ[num].ToString()); model.LJZJ = PublicTool.IsNumElseToZero(LJZJ[num].ToString()); model.GDZCJZ = PublicTool.IsNumElseToZero(GDZCJZ[num].ToString()); model.GDZCJZZB = PublicTool.IsNumElseToZero(GDZCJZZB[num].ToString()); model.GDZC = PublicTool.IsNumElseToZero(GDZC[num].ToString()); model.ZJGC = PublicTool.IsNumElseToZero(ZJGC[num].ToString()); model.GCWZ = PublicTool.IsNumElseToZero(GCWZ[num].ToString()); model.GDZCQL = PublicTool.IsNumElseToZero(GDZCQL[num].ToString()); model.SCXSWZC = PublicTool.IsNumElseToZero(SCXSWZC[num].ToString()); model.GYXSWZC = PublicTool.IsNumElseToZero(GYXSWZC[num].ToString()); model.QYZC = PublicTool.IsNumElseToZero(QYZC[num].ToString()); model.WXZC = PublicTool.IsNumElseToZero(WXZC[num].ToString()); model.KFZC = PublicTool.IsNumElseToZero(KFZC[num].ToString()); model.SY = PublicTool.IsNumElseToZero(SY[num].ToString()); model.CQDTFY = PublicTool.IsNumElseToZero(CQDTFY[num].ToString()); model.GQFZLTQ = PublicTool.IsNumElseToZero(GQFZLTQ[num].ToString()); model.DYSDSZC = PublicTool.IsNumElseToZero(DYSDSZC[num].ToString()); model.QTFLDZC = PublicTool.IsNumElseToZero(QTFLDZC[num].ToString()); model.FLDZCHJ = PublicTool.IsNumElseToZero(FLDZCHJ[num].ToString()); model.ZCZJ = PublicTool.IsNumElseToZero(ZCZJ[num].ToString()); model.DQJK = PublicTool.IsNumElseToZero(DQJK[num].ToString()); model.XZYYHJK = PublicTool.IsNumElseToZero(XZYYHJK[num].ToString()); model.XSCKJTYCF = PublicTool.IsNumElseToZero(XSCKJTYCF[num].ToString()); model.CRZJ = PublicTool.IsNumElseToZero(CRZJ[num].ToString()); model.JYXJRFZ = PublicTool.IsNumElseToZero(JYXJRFZ[num].ToString()); model.YSJRFZ = PublicTool.IsNumElseToZero(YSJRFZ[num].ToString()); model.YFPJ = PublicTool.IsNumElseToZero(YFPJ[num].ToString()); model.YFZK = PublicTool.IsNumElseToZero(YFZK[num].ToString()); model.YuSZK = PublicTool.IsNumElseToZero(YuSZK[num].ToString()); model.MCHGJRZCK = PublicTool.IsNumElseToZero(MCHGJRZCK[num].ToString()); model.YFSXFJYJ = PublicTool.IsNumElseToZero(YFSXFJYJ[num].ToString()); model.YFZGXC = PublicTool.IsNumElseToZero(YFZGXC[num].ToString()); model.YJSF = PublicTool.IsNumElseToZero(YJSF[num].ToString()); model.YFLX = PublicTool.IsNumElseToZero(YFLX[num].ToString()); model.YFGL = PublicTool.IsNumElseToZero(YFGL[num].ToString()); model.QTYJK = PublicTool.IsNumElseToZero(QTYJK[num].ToString()); model.YFBZJ = PublicTool.IsNumElseToZero(YFBZJ[num].ToString()); model.NBYFK = PublicTool.IsNumElseToZero(NBYFK[num].ToString()); model.QTYFK = PublicTool.IsNumElseToZero(QTYFK[num].ToString()); model.YTFY = PublicTool.IsNumElseToZero(YTFY[num].ToString()); model.YJLDFZ = PublicTool.IsNumElseToZero(YJLDFZ[num].ToString()); model.YFFBZK = PublicTool.IsNumElseToZero(YFFBZK[num].ToString()); model.BXHTZBJ = PublicTool.IsNumElseToZero(BXHTZBJ[num].ToString()); model.DLMMZQK = PublicTool.IsNumElseToZero(DLMMZQK[num].ToString()); model.DLCXZQK = PublicTool.IsNumElseToZero(DLCXZQK[num].ToString()); model.GJPZJS = PublicTool.IsNumElseToZero(GJPZJS[num].ToString()); model.GNPZJS = PublicTool.IsNumElseToZero(GNPZJS[num].ToString()); model.DYSY = PublicTool.IsNumElseToZero(DYSY[num].ToString()); model.YFDQZQ = PublicTool.IsNumElseToZero(YFDQZQ[num].ToString()); model.YNDDQDFLDFZ = PublicTool.IsNumElseToZero(YNDDQDFLDFZ[num].ToString()); model.QTLDFZ = PublicTool.IsNumElseToZero(QTLDFZ[num].ToString()); model.LDFZHJ = PublicTool.IsNumElseToZero(LDFZHJ[num].ToString()); model.CQJQ = PublicTool.IsNumElseToZero(CQJQ[num].ToString()); model.YFZQ = PublicTool.IsNumElseToZero(YFZQ[num].ToString()); model.CQYFZQ = PublicTool.IsNumElseToZero(CQYFZQ[num].ToString()); model.ZXYFK = PublicTool.IsNumElseToZero(ZXYFK[num].ToString()); model.YJFLDFZ = PublicTool.IsNumElseToZero(YJFLDFZ[num].ToString()); model.CQDYSY = PublicTool.IsNumElseToZero(CQDYSY[num].ToString()); model.DYSDSFZ = PublicTool.IsNumElseToZero(DYSDSFZ[num].ToString()); model.QTFLDFZ = PublicTool.IsNumElseToZero(QTFLDFZ[num].ToString()); model.FLDFZHJ = PublicTool.IsNumElseToZero(FLDFZHJ[num].ToString()); model.FZHJ = PublicTool.IsNumElseToZero(FZHJ[num].ToString()); model.SSZB = PublicTool.IsNumElseToZero(SSZB[num].ToString()); model.ZBGJ = PublicTool.IsNumElseToZero(ZBGJ[num].ToString()); model.JKCG = PublicTool.IsNumElseToZero(JKCG[num].ToString()); model.ZXCB = PublicTool.IsNumElseToZero(ZXCB[num].ToString()); model.YYGJ = PublicTool.IsNumElseToZero(YYGJ[num].ToString()); model.YBFXZB = PublicTool.IsNumElseToZero(YBFXZB[num].ToString()); model.WQDDTZSS = PublicTool.IsNumElseToZero(WQDDTZSS[num].ToString()); model.WFPLR = PublicTool.IsNumElseToZero(WFPLR[num].ToString()); model.NFPXJGL = PublicTool.IsNumElseToZero(NFPXJGL[num].ToString()); model.WBBBZSCE = PublicTool.IsNumElseToZero(WBBBZSCE[num].ToString()); model.GSYMGSGDQYHJ = PublicTool.IsNumElseToZero(GSYMGSGDQYHJ[num].ToString()); model.SSGDQY = PublicTool.IsNumElseToZero(SSGDQY[num].ToString()); model.SYZQY = PublicTool.IsNumElseToZero(SYZQY[num].ToString()); model.FZHSYZQY = PublicTool.IsNumElseToZero(FZHSYZQY[num].ToString()); if (_StockZCFZService.Add(model) == 0) { continue; } } } MessageBox.Show("结束!"); }
//财务指标 private void button2_Click(object sender, EventArgs e) { List <StockInfo> stocklist = _Stockservice.GetModelList(""); foreach (StockInfo s in stocklist) { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = "http://quotes.money.163.com/service/zycwzb_" + s.stockcode + ".html?type=report"; item.Encoding = Encoding.UTF8; item.Method = "GET"; item.Timeout = 100000; item.ReadWriteTimeout = 30000;//写入Post数据超时时间,可选项默认为30000 HttpResult result = http.GetHtml(item); string Result = result.Html.Replace("\r\n\t", "").Replace(" ", ""); //string[] arrTemp = result.Html.Split('\r\n'); string[] strlist = Result.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (strlist.Length == 1) { continue; } string[] reportDate = strlist[0].Substring(0, strlist[0].Length - 1).Split(','); string[] JBMGSY = strlist[1].Substring(0, strlist[1].Length - 1).Split(','); string[] MGJZC = strlist[2].Substring(0, strlist[2].Length - 1).Split(','); string[] MGJYHDCSXJLJE = strlist[3].Substring(0, strlist[3].Length - 1).Split(','); string[] ZYYWSR = strlist[4].Substring(0, strlist[4].Length - 1).Split(','); string[] ZYYWLR = strlist[5].Substring(0, strlist[5].Length - 1).Split(','); string[] YYLR = strlist[6].Substring(0, strlist[6].Length - 1).Split(','); string[] TZSY = strlist[7].Substring(0, strlist[7].Length - 1).Split(','); string[] YYEYSZJE = strlist[8].Substring(0, strlist[8].Length - 1).Split(','); string[] LRZE = strlist[9].Substring(0, strlist[9].Length - 1).Split(','); string[] JLR = strlist[10].Substring(0, strlist[10].Length - 1).Split(','); string[] JLROUT = strlist[11].Substring(0, strlist[11].Length - 1).Split(','); string[] JYHDCSDXJLJE = strlist[12].Substring(0, strlist[12].Length - 1).Split(','); string[] XJJXJDJWJCJE = strlist[13].Substring(0, strlist[13].Length - 1).Split(','); string[] ZZC = strlist[14].Substring(0, strlist[14].Length - 1).Split(','); string[] LDZC = strlist[15].Substring(0, strlist[15].Length - 1).Split(','); string[] ZFZ = strlist[16].Substring(0, strlist[16].Length - 1).Split(','); string[] LDFZ = strlist[17].Substring(0, strlist[17].Length - 1).Split(','); string[] GDQYBHSSGDQY = strlist[18].Substring(0, strlist[18].Length - 1).Split(','); string strlist19 = strlist[19].Replace("\t", ""); string[] JZCSYLJQ = strlist19.Substring(0, strlist19.Length - 1).Split(','); for (int num = 1; num < reportDate.Length; num++) { int IsHave = _StockCwservice.GetRecordCount("Code='" + s.stockcode + "' AND ReportDate=CONVERT(datetime,'" + reportDate[num].ToString() + "',102)"); if (IsHave != 0) { continue; } StockCwInfo cw = new StockCwInfo(); cw.Code = s.stockcode; cw.ReportDate = Convert.ToDateTime(reportDate[num].ToString()); cw.JBMGSY = decimal.Parse(PublicTool.IsNumElseToZero(JBMGSY[num].ToString())); cw.MGJZC = decimal.Parse(PublicTool.IsNumElseToZero(MGJZC[num].ToString())); cw.MGJYHDCSXJLJE = decimal.Parse(PublicTool.IsNumElseToZero(MGJYHDCSXJLJE[num].ToString())); cw.ZYYWSR = PublicTool.IsNumElseToZero(ZYYWSR[num].ToString()); cw.ZYYWLR = PublicTool.IsNumElseToZero(ZYYWLR[num].ToString()); cw.YYLR = PublicTool.IsNumElseToZero(YYLR[num].ToString()); cw.TZSY = PublicTool.IsNumElseToZero(TZSY[num].ToString()); cw.YYEYSZJE = PublicTool.IsNumElseToZero(YYEYSZJE[num].ToString()); cw.LRZE = PublicTool.IsNumElseToZero(LRZE[num].ToString()); cw.JLR = PublicTool.IsNumElseToZero(JLR[num].ToString()); cw.JLROUT = PublicTool.IsNumElseToZero(JLROUT[num].ToString()); cw.JYHDCSDXJLJE = PublicTool.IsNumElseToZero(JYHDCSDXJLJE[num].ToString()); cw.XJJXJDJWJCJE = PublicTool.IsNumElseToZero(XJJXJDJWJCJE[num].ToString()); cw.ZZC = PublicTool.IsNumElseToZero(ZZC[num].ToString()); cw.LDZC = PublicTool.IsNumElseToZero(LDZC[num].ToString()); cw.ZFZ = PublicTool.IsNumElseToZero(ZFZ[num].ToString()); cw.LDFZ = PublicTool.IsNumElseToZero(LDFZ[num].ToString()); cw.GDQYBHSSGDQY = PublicTool.IsNumElseToZero(GDQYBHSSGDQY[num].ToString()); cw.JZCSYLJQ = decimal.Parse(PublicTool.IsNumElseToZero(JZCSYLJQ[num].ToString())); if (_StockCwservice.Add(cw) == 0) { continue; } } } MessageBox.Show("结束!"); }
/// <summary> /// 模拟登陆,ip代理可能需要用到 /// </summary> /// <returns></returns> public bool SimulateLogin() { return(true); IPProxy ipProxy = null; HttpHelper http = new HttpHelper(); //尝试登陆 while (true) { try { ipProxy = Settings.GetIPProxy(); if (ipProxy == null || string.IsNullOrEmpty(ipProxy.IP)) { Settings.SimulateCookies = string.Empty; //return true; } HttpItem item = new HttpItem() { URL = "https://passport.fang.com/login.api", //URL 必需项 Encoding = null, //编码格式(utf-8,gb2312,gbk) 可选项 默认类会自动识别 //Encoding = Encoding.Default, Method = "post", //URL 可选项 默认为Get //Timeout = 100000,//连接超时时间 可选项默认为100000 //ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000 //IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写 //Cookie = "",//字符串Cookie 可选项 UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko", //用户的浏览器类型,版本,操作系统 可选项有默认值 Accept = "text/html, application/xhtml+xml, */*", // 可选项有默认值 ContentType = "application/x-www-form-urlencoded", //返回类型 可选项有默认值 Referer = "https://passport.fang.com/", //来源URL 可选项 Postdata = "Uid=luckymn&Pwd=1c523e9b2109407d0857676dfc20af997c14791f495ec8676979628bfef0762ce2679e2f4770d536526bcf00639ec803539f02c54387fbd4a3f159ec5a6185cd46cb139b5c2696c269bce5b7f9c00fb3a9bc58e815773c227b54d4570da0cbee50b47b29c363d398791d3065c0343494aebaa925313e705fd514898e56c2df29&Service=soufun-passport-web&IP=&VCode=&AutoLogin=1", Allowautoredirect = true, }; if (ipProxy != null) { item.ProxyIp = ipProxy.IP; } Console.WriteLine(string.Format("尝试登陆{0}", Settings.curIPProxy != null ? Settings.curIPProxy.IP : string.Empty)); HttpResult result = http.GetHtml(item); string cookie = string.Empty; foreach (CookieItem s in HttpCookieHelper.GetCookieList(result.Cookie)) { //if (s.Key.Contains("24a79_")) { cookie += HttpCookieHelper.CookieFormat(s.Key, s.Value); } } if (result.Html.IndexOf("luckymn") > 0) { Settings.SimulateCookies = cookie;//设置cookie值 Console.WriteLine("zluckymn模拟登陆成功"); return(true); } return(false); } catch (WebException ex) { IPInvalidProcess(ipProxy); } catch (Exception ex) { IPInvalidProcess(ipProxy); } } }
/// <summary> /// 发送请求 /// </summary> /// <param name="cmd"></param> private void SendCommand(Command cmd) { //------------------ LabRequest request = new LabRequest(); request.DeviceID = abaxisDevice.FuseID; request.TestCode = abaxisDevice.RequestCode;// "HEM"; //----------这是什么? LabRequests labRequests = new LabRequests(); labRequests.LabRequest = new List <LabRequest>(); labRequests.LabRequest.Add(request); //宠物信息 AnimalDetails deatails = new AnimalDetails(); deatails.AnimalID = cmd.PetId; deatails.AnimalName = cmd.PetName; deatails.Breed = cmd.KindOf.ToString(); //----------品种 这里是可以随意填还是有固定值? deatails.Gender = cmd.Gender.ToString(); // "female";//性别 deatails.Species = cmd.KindOf.ToString(); // "DOG";//----------种类 这里是可以随意填还是有固定值? int age = Convert.ToInt32(cmd.Age); deatails.Age = string.Format("{0}Y{1}M0D", age / 12, age % 12); // "1Y3M25D"; deatails.DateOfBirth = DateTime.Now.AddMonths(-age).ToString("yyyy-MM"); // "2018-08-11"; deatails.AbbreviatedHistory = ""; //主人信息 Identification identifiObj = new Identification(); identifiObj.ReportType = "Request"; identifiObj.PracticeID = cmd.Id; // ----------这两个字段是做什么的? identifiObj.PracticeRef = cmd.Code; // ----------这两个字段是做什么的? identifiObj.LaboratoryID = cmd.Id; identifiObj.PIMSName = ""; //----------这个是什么 identifiObj.OwnerID = cmd.CustomerId; identifiObj.OwnerName = cmd.Customer; identifiObj.VetID = cmd.DoctorId; // "8888";//医生ID identifiObj.VetName = cmd.Doctor; // //医生姓名 LabReport report = new LabReport(); report.LabRequests = labRequests; report.AnimalDetails = deatails; report.Identification = identifiObj; string xmlStr = string.Empty; string xmlmessage = string.Empty; using (StringWriter sw = new StringWriter()) { MemoryStream ms = new MemoryStream(); XmlTextWriter textwriter = new XmlTextWriter(ms, Encoding.GetEncoding("UTF-8")); XmlSerializer xz = new XmlSerializer(typeof(LabReport)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); xz.Serialize(textwriter, report, ns); xmlmessage = Encoding.UTF8.GetString(ms.GetBuffer()); //xz.Serialize(sw, report, ns); xmlStr = xmlmessage; } HttpItem httpItem = CreateHttpItem(@"vetsync/v1/orders"); httpItem.Method = HttpMethod.Post; httpItem.Content = Encoding.UTF8.GetBytes(xmlStr); HttpResult <string> result = WebLogic.GetHttpResult <string>(httpItem); if (result.Success) { //result.Result //----------此字段为返回的数据! 返回什么表示成功???? } }
/// <summary> /// 设置证书 /// </summary> /// <param name="item"></param> private void SetCer(HttpItem item) { if (!string.IsNullOrWhiteSpace(item.CerPath)) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(item.URL); SetCerList(item); //将证书添加到请求里 request.ClientCertificates.Add(new X509Certificate(item.CerPath)); } else { //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(item.URL); SetCerList(item); } }
public static void EmpireCMS() { string strIMSPhone = System.Web.HttpUtility.UrlEncode("尹华伟"); string strIMSPwd = "yinhuawei"; string postData = "enews=login&username="******"&password=yinhuawei&equestion=0&eanswer=&adminwindow=0&imageField.x=36&imageField.y=13"; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/ecmsadmin.php", Method = "post", Postdata = postData, Host = "www.honghezdh.com", Referer = "http://www.honghezdh.com/e/admin", UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "application/x-www-form-urlencoded", KeepAlive = true, Allowautoredirect = true, AutoRedirectCookie = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); HttpResult result = http.GetHtml(item); string html = result.Html; string code = HttpHelper.GetBetweenHtml(html, "LoginSuccess&", "\">"); // string codeUse = code.Substring(code.IndexOf("=") + 1, code.Length - code.IndexOf("=") - 1); string loginUrl = HttpHelper.GetBetweenHtml(html, "url=", "\">"); string cookies = result.Cookie; cookies = cookies.Replace("path=/,guqyscheckkey=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0;", ""); cookies = cookies.Replace("path=/,guqysecertkeyrnds=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0;", ""); cookies = cookies.Replace("path=/", ""); cookies = cookies.Replace(",", ""); cookies = cookies.Replace(" ", ""); http = new HttpHelper(); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/" + loginUrl, Method = "get", Host = "www.honghezdh.com", Cookie = cookies, Referer = "http://www.honghezdh.com/e/admin/ecmsadmin.php", UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true, Allowautoredirect = true, AutoRedirectCookie = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string cookiesNew = result.Cookie; cookiesNew = cookiesNew.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNew); http = new HttpHelper(); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/admin.php?" + code, Method = "get", Host = "www.honghezdh.com", Cookie = cookies, Referer = "http://www.honghezdh.com/e/admin/" + loginUrl, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string htmlNew = result.Html; string code2 = HttpHelper.GetBetweenHtml(htmlNew, "rhash", "'"); string cookiesNewOne = result.Cookie; cookiesNewOne = cookiesNew.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewOne); http = new HttpHelper(); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/DoTimeRepage.php?" + code + "&rhash" + code2, Method = "get", Host = "www.honghezdh.com", Cookie = cookies, Referer = "http://www.honghezdh.com/e/admin/admin.php?" + code, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string cookiesNewTwo = result.Cookie; cookiesNewTwo = cookiesNew.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewTwo); http = new HttpHelper(); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/AddInfoChClass.php?" + code, Method = "get", Host = "www.honghezdh.com", Cookie = cookies, Referer = "http://www.honghezdh.com/e/admin/admin.php?" + code, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string cookiesNewThree = result.Cookie; cookiesNewThree = cookiesNew.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewThree); http = new HttpHelper(); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/AddNews.php?" + code + "&enews=AddNews&classid=39", Method = "get", Host = "www.honghezdh.com", Cookie = cookies, Referer = "http://www.honghezdh.com/e/admin/AddInfoChClass.php", UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string cookiesNewFour = result.Cookie; cookiesNewFour = cookiesNewThree.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewFour); //-----------------文章发布---------------------------------------------------------------------------------------------------------------- string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")); // 分界线可以自定义参数 var rhash = HttpHelper.GetBetweenHtml(result.Html, "ecmsinfo.php?", "&enews=AddInfoToReHtml").Replace("?&" + code + "&", ""); string format = "--" + boundary + "\r\nContent-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}\r\n"; //自带项目分隔符 var ehash = code.Split('='); var rhashs = rhash.Split('='); var filePass = Tool.GetInputVal(result.Html, @"<input type=hidden value=""(?<value>[^""]+)"" name=filepass>"); StringBuilder paramImg = new StringBuilder(); paramImg.Append("--" + boundary + "\r\n"); paramImg.Append(string.Format(@"Content-Disposition: form-data; name=""file""; filename=""test.jpg""" + "\r\n")); paramImg.Append(string.Format(@"Content-Type: image/jpeg" + "\r\n\r\n")); byte[] HeadBytes = Encoding.ASCII.GetBytes(paramImg.ToString()); #region 图片数据 byte[] PicBytes = Tool.ImageToBytesFromFilePath(string.Format(@"E:\\test.jpg")); #endregion paramImg.Append(string.Format(format, ehash[0], ehash[1])); paramImg.Append(string.Format(format, rhashs[0], rhashs[1])); paramImg.Append(string.Format(format, "enews", "TranFile")); paramImg.Append(string.Format(format, "classid", "15")); paramImg.Append(string.Format(format, "infoid", "0")); paramImg.Append(string.Format(format, "filepass", filePass)); paramImg.Append(string.Format(format, "ecmsfrom", "http://www.honghezdh.com/e/admin/ecmsinfo.php")); paramImg.Append(string.Format(format, "type", "1")); paramImg.Append(string.Format(format, "doing", "1")); paramImg.Append(string.Format(format, "no", "test.jpg")); paramImg.Append(string.Format(format, "width", "105")); paramImg.Append(string.Format(format, "height", "118")); paramImg.Append(string.Format(format, "tranurl", "http://")); paramImg.Append(string.Format(format, "modtype", "0")); paramImg.Append(string.Format(format, "sinfo", "1")); paramImg.Append(string.Format(format, "fstb", "1")); paramImg.Append(string.Format(format, "Submit3", "上传")); paramImg.Append("\r\n" + boundary + "--\r\n"); byte[] TailBytesi = Encoding.ASCII.GetBytes(paramImg.ToString()); var UploadBuffers = Tool.ComposeArrays(HeadBytes, PicBytes); UploadBuffers = Tool.ComposeArrays(UploadBuffers, TailBytesi); http = new HttpHelper(); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/ecmseditor/ecmseditor.php", Method = "post", Host = "www.honghezdh.com", Cookie = cookies, Referer = "http://www.honghezdh.com/e/admin/ecmseditor/file.php?&classid=15&infoid=0&filepass="******"&type=1&modtype=0&sinfo=1&doing=1&tranfrom=&field=titlepic&" + code, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "multipart/form-data; boundary=" + boundary, PostDataType = PostDataType.Byte, PostEncoding = Encoding.UTF8, PostdataByte = UploadBuffers, }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/ecmseditor/file.php?&classid=15&infoid=0&filepass="******"&type=1&modtype=0&sinfo=1&doing=1&tranfrom=&field=titlepic&" + code, Method = "get", Host = "www.honghezdh.com", Cookie = cookies, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "multipart/form-data; boundary=" + boundary, PostDataType = PostDataType.Byte, PostEncoding = Encoding.UTF8, PostdataByte = UploadBuffers, }; html = http.GetHtml(item).Html; var imgPath = HttpHelper.GetBetweenHtml(html, "index.html", "target='_blank'").Replace("?url=", "").Trim(); //--------------------图片上传结束--------------------------------------------------------------------------------------------------------- StringBuilder param = new StringBuilder(); param.Append("--" + boundary + "\r\n"); param.Append(string.Format(format, ehash[0], ehash[1])); param.Append(string.Format(format, rhashs[0], rhashs[1])); param.Append(string.Format(format, "enews", "AddNews")); param.Append(string.Format(format, "classid", "15")); param.Append(string.Format(format, "bclassid", "10")); param.Append(string.Format(format, "id", "0")); param.Append(string.Format(format, "filepass", filePass)); param.Append(string.Format(format, "ecmsfrom", "http://www.honghezdh.com/e/admin/ecmsinfo.php")); param.Append(string.Format(format, "ecmsnfrom", "1")); param.Append(string.Format(format, "title", "2018ttttt啊啊啊啊啊啊啊ttttttt")); param.Append(string.Format(format, "ftitle", "seo标题三生三世")); param.Append(string.Format(format, "checked", "1")); param.Append(string.Format(format, "newstime", DateTime.Now.ToString())); param.Append(string.Format(format, "newstext", "文章详情2018")); param.Append(string.Format(format, "dokey", "1")); param.Append(string.Format(format, "titlepic", imgPath)); param.Append(string.Format(format, "addnews", "提 交 ")); param.Append("\r\n--" + boundary + "--\r\n"); byte[] TailBytes = Encoding.UTF8.GetBytes(param.ToString()); http = new HttpHelper(); item = new HttpItem { URL = "http://www.honghezdh.com/e/admin/ecmsinfo.php", Method = "post", Host = "www.honghezdh.com", Cookie = cookies, // Referer = "http://www.honghezdh.com/e/admin/AddNews.php?enews=AddNews&ecmsnfrom=1&bclassid=10&classid=15&" + code, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "multipart/form-data; boundary=" + boundary, PostDataType = PostDataType.Byte, PostEncoding = Encoding.UTF8, PostdataByte = TailBytes, }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); html = result.Html; }
/// <summary> /// 设置代理 /// </summary> /// <param name="item">参数对象</param> private void SetProxy(HttpItem item) { if (!string.IsNullOrWhiteSpace(item.ProxyIp)) { //设置代理服务器 if (item.ProxyIp.Contains(":")) { string[] plist = item.ProxyIp.Split(':'); WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim())); //建议连接 myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd); //给当前请求对象 request.Proxy = myProxy; } else { WebProxy myProxy = new WebProxy(item.ProxyIp, false); //建议连接 myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd); //给当前请求对象 request.Proxy = myProxy; } request.Credentials = CredentialCache.DefaultCredentials; } else if (item.WebProxy != null) { request.Proxy = item.WebProxy; } }
public static Task<List<string>> GetCookie(string username, string passWord, string host,string adminHost) { return Task<string>.Run(() => { string postData = "enews=login&username="******"&password="******"&equestion=0&eanswer=&adminwindow=0&imageField.x=36&imageField.y=13"; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem { URL = host + adminHost + "ecmsadmin.php", Method = "post", Postdata = postData, // Host = "www.honghezdh.com", Referer = host + adminHost, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "application/x-www-form-urlencoded", KeepAlive = true, Allowautoredirect = true, AutoRedirectCookie = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); HttpResult result = http.GetHtml(item); string html = result.Html; string code = HttpHelper.GetBetweenHtml(html, "LoginSuccess&", "\">"); string loginUrl = HttpHelper.GetBetweenHtml(html, "url=", "\">"); string cookies = result.Cookie; cookies = cookies.Replace("path=/,guqyscheckkey=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0;", ""); cookies = cookies.Replace("path=/,guqysecertkeyrnds=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0;", ""); cookies = cookies.Replace("path=/", ""); cookies = cookies.Replace(",", ""); cookies = cookies.Replace(" ", ""); item = new HttpItem { URL = host + adminHost + loginUrl, Method = "get", Cookie = cookies, Referer = host + adminHost+ "ecmsadmin.php", UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true, Allowautoredirect = true, AutoRedirectCookie = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); html = result.Html; if (html.IndexOf("登录成功") > -1) { var cookiesNew = cookies.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNew); http = new HttpHelper(); item = new HttpItem { URL = host + adminHost+"admin.php?" + code, Method = "get", Cookie = cookies, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string htmlNew = result.Html; string code2 = "rhash"+HttpHelper.GetBetweenHtml(htmlNew, "rhash", "'"); string cookiesNewOne = result.Cookie; cookiesNewOne = cookiesNew.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewOne); http = new HttpHelper(); item = new HttpItem { URL = host + adminHost+ "DoTimeRepage.php?" + code + "&"+code2, Method = "get", Cookie = cookies, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string cookiesNewTwo = result.Cookie; cookiesNewTwo = cookiesNew.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewTwo); http = new HttpHelper(); item = new HttpItem { URL = host + adminHost+ "AddInfoChClass.php?" + code, Method = "get", Cookie = cookies, Referer = host + adminHost+"admin.php?" + code, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); string cookiesNewThree = result.Cookie; cookiesNewThree = cookiesNew.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewThree); List<string> cookiesList = new List<string>(); cookiesList.Add(cookies); cookiesList.Add(cookiesNewThree); cookiesList.Add(code); cookiesList.Add(code2); return cookiesList; } else { return null; } }); }
public string Excute(WeChatConfig config, WeChatData data) { //var msg = new //{ // button = new object[] // { // new {type="click",name="定位监控",key="CLICK_DWJK" }, // new {type="click",name="里程报表",key="CLICK_LCBB" }, // new // { // name="账号", // sub_button=new object[] // { // new {type="click",name="绑定账号",key="CLICK_BDZH" }, // new {type="click",name="取消绑定",key="CLICK_QXBD" } // } // } // } //}; var msg = new { button = new object[] { new { name="查车", sub_button=new object[] { new {type="click",name="定位监控",key="CLICK_DWJK" }, new {type="click",name="里程报表",key="CLICK_LCBB" } } }, new {type="click",name="设防撤防",key="CLICK_SFCF" }, new { name="账号", sub_button=new object[] { new {type="click",name="绑定账号",key="CLICK_BDZH" }, new {type="click",name="取消绑定",key="CLICK_QXBD" } } } } }; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = string.Format("{0}menu/create?access_token={1}", config.APIUrl, config.AccessToken); item.Method = "POST"; item.Encoding = "utf-8"; item.ContentType = "application/x-www-form-urlencoded"; item.Postdata = msg.ToJsonString(); string result = http.GetHtml(item); return result; }
public static Task<string> postECMS(string cookies,string cookiesNewThree, string code,string code2, string webCategoryCode, string host, string typeId, string title, string keyWord, string context, string adminHost,string imgUrl) { return Task<string>.Run(() => { var ehash = code.Split('='); var rhashs =code2.Split('='); var http = new HttpHelper(); var item = new HttpItem { URL = host + adminHost+"AddNews.php?" + code + "&enews=AddNews&classid=" + typeId, Method = "get", Cookie = cookies, Referer = host + adminHost+"AddInfoChClass.php", UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", KeepAlive = true }; item.Header.Add("Upgrade-Insecure-Requests", "1"); var result = http.GetHtml(item); string cookiesNewFour = result.Cookie; cookiesNewFour = cookiesNewThree.Replace("path=/", ""); cookies = Tool.updateCookie(cookies, cookiesNewFour); //-----------------文章发布---------------------------------------------------------------------------------------------------------------- string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")); // 分界线可以自定义参数 var filePass = Tool.GetInputVal(result.Html, @"<input type=hidden value=""(?<value>[^""]+)"" name=filepass>"); string format = "--" + boundary + "\r\nContent-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}\r\n"; //自带项目分隔符 var imgPath = string.Empty; if (!string.IsNullOrEmpty(imgUrl)) { StringBuilder paramImg = new StringBuilder(); paramImg.Append("--" + boundary + "\r\n"); paramImg.Append(string.Format(@"Content-Disposition: form-data; name=""file""; filename=""test.jpg""" + "\r\n")); paramImg.Append(string.Format(@"Content-Type: image/jpeg" + "\r\n\r\n")); byte[] HeadBytes = Encoding.ASCII.GetBytes(paramImg.ToString()); #region 图片数据 byte[] PicBytes = Tool.ImageToBytesFromFilePath(string.Format(@"E:\\test.jpg")); #endregion paramImg.Append(string.Format(format, ehash[0], ehash[1])); paramImg.Append(string.Format(format, rhashs[0], rhashs[1])); paramImg.Append(string.Format(format, "enews", "TranFile")); paramImg.Append(string.Format(format, "classid", "15")); paramImg.Append(string.Format(format, "infoid", "0")); paramImg.Append(string.Format(format, "filepass", filePass)); paramImg.Append(string.Format(format, "ecmsfrom", host + adminHost+"ecmsinfo.php")); paramImg.Append(string.Format(format, "type", "1")); paramImg.Append(string.Format(format, "doing", "1")); paramImg.Append(string.Format(format, "no", "test.jpg")); paramImg.Append(string.Format(format, "width", "105")); paramImg.Append(string.Format(format, "height", "118")); paramImg.Append(string.Format(format, "tranurl", "http://")); paramImg.Append(string.Format(format, "modtype", "0")); paramImg.Append(string.Format(format, "sinfo", "1")); paramImg.Append(string.Format(format, "fstb", "1")); paramImg.Append(string.Format(format, "Submit3", "上传")); paramImg.Append("\r\n" + boundary + "--\r\n"); byte[] TailBytesi = Encoding.ASCII.GetBytes(paramImg.ToString()); var UploadBuffers = Tool.ComposeArrays(HeadBytes, PicBytes); UploadBuffers = Tool.ComposeArrays(UploadBuffers, TailBytesi); http = new HttpHelper(); item = new HttpItem { URL = host + adminHost+"ecmseditor/ecmseditor.php", Method = "post", Cookie = cookies, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "multipart/form-data; boundary=" + boundary, PostDataType = PostDataType.Byte, PostEncoding = Encoding.UTF8, PostdataByte = UploadBuffers, }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); item = new HttpItem { URL = host + adminHost+"ecmseditor/file.php?&classid=15&infoid=0&filepass="******"&type=1&modtype=0&sinfo=1&doing=1&tranfrom=&field=titlepic&" + code, Method = "get", Cookie = cookies, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "multipart/form-data; boundary=" + boundary, PostDataType = PostDataType.Byte, PostEncoding = Encoding.UTF8, PostdataByte = UploadBuffers, }; var html = http.GetHtml(item).Html; imgPath = HttpHelper.GetBetweenHtml(html, "index.html", "target='_blank'").Replace("?url=", "").Trim(); Thread.Sleep(2000); } //--------------------图片上传结束--------------------------------------------------------------------------------------------------------- StringBuilder param = new StringBuilder(); param.Append("--" + boundary + "\r\n"); param.Append(string.Format(format, ehash[0], ehash[1])); param.Append(string.Format(format, rhashs[0], rhashs[1])); param.Append(string.Format(format, "enews", "AddNews")); param.Append(string.Format(format, "classid", typeId)); param.Append(string.Format(format, "id", "0")); param.Append(string.Format(format, "filepass", filePass)); param.Append(string.Format(format, "ecmsfrom", host + adminHost+"ecmsinfo.php")); param.Append(string.Format(format, "ecmsnfrom", "1")); param.Append(string.Format(format, "title", title)); param.Append(string.Format(format, "ftitle", title+","+keyWord)); param.Append(string.Format(format, "checked", "1")); // param.Append(string.Format(format, "newstime", DateTime.Now.ToString())); param.Append(string.Format(format, "newstext", context)); param.Append(string.Format(format, "dokey", "1")); param.Append(string.Format(format, "titlepic", imgPath)); param.Append(string.Format(format, "addnews", "提 交 ")); param.Append("\r\n--" + boundary + "--\r\n"); byte[] TailBytes = Encoding.UTF8.GetBytes(param.ToString()); http = new HttpHelper(); item = new HttpItem { URL = host + adminHost+"ecmsinfo.php", Method = "post", Cookie = cookies, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", ContentType = "multipart/form-data; boundary=" + boundary, PostDataType = PostDataType.Byte, PostEncoding = Encoding.UTF8, PostdataByte = TailBytes, }; item.Header.Add("Upgrade-Insecure-Requests", "1"); result = http.GetHtml(item); Thread.Sleep(2500); return result.Html; }); }
/// <summary> /// 快速Post数据这个访求与GetHtml一样,只是不接收返回数据,只做提交。 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> internal HttpResult FastRequest(HttpItem item) { return httpbase.FastRequest(item); }
public TOut HttpPostData <TIn, TOut>(string url, TIn objIn, HttpItem pitem = null) { var resultStr = HttpPostData(url, objIn, pitem); return(CommunicationHelper.DeserializeJsonToObj <TOut>(resultStr)); }
/// <summary> /// 手机短信(批量发送) /// 注意:批量发送只验证平台发送总数限制; /// </summary> /// <param name="mobiles">手机号码,以英文“,”逗号分隔开</param> /// <param name="content">短信内容</param> /// <param name="pass">无作用(备用)</param> /// <param name="msg">返回提示信息</param> /// <returns>bool</returns> public bool MultiSend(string mobiles, string content, int pass, out string msg) { msg = ""; if (string.IsNullOrEmpty(this.username) || string.IsNullOrEmpty(this.password)) { msg = "短信配置参数有误,请完善后再提交!"; return(false); } int sucCount = 0; string[] oldMobileArr = mobiles.Split(','); //查询是否超出平台限制 int thisSafeTotal = new BLL.sms_log().GetCurDayCount(); if (this.safeTotal > 0 && (thisSafeTotal > this.safeTotal || thisSafeTotal + oldMobileArr.Length > this.safeTotal)) { msg = "对不起,平台短信发送量已超出最大限制!"; return(false); } //错误消息 string errorMsg = string.Empty; //验证手机号码 Regex r = new Regex(@"^1(3|4|5|7|8)\d{9}$", RegexOptions.IgnoreCase); //2000条为一批,求出分多少批 int batch = oldMobileArr.Length / 2000 + 1; for (int i = 0; i < batch; i++) { StringBuilder sb = new StringBuilder(); int sendCount = 0; //发送数量 int maxLenght = (i + 1) * 2000; //循环最大的数 //检测号码,忽略不合格的,重新组合 for (int j = 0; j < oldMobileArr.Length && j < maxLenght; j++) { int arrNum = j + (i * 2000); string mobile = oldMobileArr[arrNum].Trim(); Match m = r.Match(mobile); //搜索匹配项 if (m != null) { sendCount++; sb.Append(mobile + ","); } } //发送短信 if (sb.ToString().Length > 0) { try { StringBuilder parameter = new StringBuilder(); parameter.AppendFormat("SpCode={0}&LoginName={1}&Password={2}&MessageContent={3}", this.apicode, this.username, this.password, content); parameter.AppendFormat("&UserNumber={0}&SerialNumber={1}&ScheduleTime=&ExtendAccessNum=&f=1", Utils.DelLastComma(sb.ToString()), DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001"); byte[] data = Encoding.GetEncoding("GBK").GetBytes(parameter.ToString()); HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://sms.api.ums86.com:8899/sms/Api/Send.do", Encoding = Encoding.Default, Method = "POST", ContentType = "application/x-www-form-urlencoded", PostdataByte = data }; HttpResult result = http.GetHtml(item); //获取返回值 string code = QueryParameter(result.Html, "result"); //获取描述 errorMsg = QueryParameter(result.Html, "description"); if (string.IsNullOrEmpty(errorMsg)) { //加载错误表 Dictionary <string, string> dic = ErrorDic(); if (dic.ContainsKey(code)) { errorMsg = dic[code]; } } if (code == "0") { sucCount++; } } catch (Exception ex) { errorMsg = "提交失败,错误提示:" + ex.Message; } } } //返回状态 if (sucCount > 0) { msg = "成功提交" + sucCount + "条,失败" + (oldMobileArr.Length - sucCount) + "条"; return(true); } msg = errorMsg; return(false); }
public string HttpPostData(string url, object paramter, HttpItem pitem = null) { try { if (paramter == null) { CommunicationHelper.TraceException("HttppostData", "null parameter", "parameter obj is null"); } string contentType = "application/json"; if (paramter is string || paramter.GetType().IsValueType) { contentType = "application/x-www-form-urlencoded"; } var postStr = CommunicationHelper.SerializeObjToJsonStr(paramter); if (pitem == null) { pitem = new HttpItem() { Method = "Post", Postdata = postStr, ContentType = "application/x-www-form-urlencoded", Encoding = Encoding.UTF8, URL = url }; } else { pitem.Method = "Post"; pitem.Postdata = postStr; //pitem.ContentType = contentType; pitem.URL = url; } var result = _engine.GetHtml(pitem); CommunicationHelper.RecordTrace("HttpPostData", "返回结果:" + result.Html); return(result.Html); //var client = new KRWebClient { Timeout = Timeout }; //NameValueCollection param = new NameValueCollection(); ////client.Headers.Add("Content-Type", "application/json"); //param.Add("type", type); //param.Add("content", jsonStr); //var content = client.UploadValues(url, param); //var strContent = Encoding.UTF8.GetString(content); //return strContent; //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //request.Method = "Post"; //request.ContentType = "application/json"; //request.ContentLength = _encoding.GetByteCount(jsonStr); //Stream myRequestStream = request.GetRequestStream(); //StreamWriter myStreamWriter = new StreamWriter(myRequestStream); //myStreamWriter.Write(jsonStr); //myStreamWriter.Close(); //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //Stream myResponseStream = response.GetResponseStream(); //StreamReader myStreamReader = new StreamReader(myResponseStream); //retString = myStreamReader.ReadToEnd(); //myStreamReader.Close(); //myResponseStream.Close(); } catch (Exception ex) { ClientHelper.TraceException("HttpPostData:Post", "Post数据失败", ex.Message); return(string.Empty); } //return HttpUtility.UrlDecode(retString); }
/// <summary> /// 为请求准备参数 /// </summary> ///<param name="item">参数列表</param> private void SetRequest(HttpItem item) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; //初始化对像,并设置请求的URL地址 _request = (HttpWebRequest)WebRequest.Create(item.Url); // 验证证书 SetCer(item); SetCerList(item); //设置Header参数 if (item.Header != null && item.Header.Count > 0) { foreach (var key in item.Header.AllKeys) { _request.Headers.Add(key, item.Header[key]); } } // 设置代理 SetProxy(item); if (item.ProtocolVersion != null) { _request.ProtocolVersion = item.ProtocolVersion; } _request.ServicePoint.Expect100Continue = item.Expect100Continue; //请求方式Get或者Post _request.Method = item.Method; _request.Timeout = item.Timeout; _request.KeepAlive = item.KeepAlive; _request.ReadWriteTimeout = item.ReadWriteTimeout; if (!string.IsNullOrWhiteSpace(item.Host)) { _request.Host = item.Host; } if (item.IfModifiedSince != null) { _request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince); } //Accept _request.Accept = item.Accept; //ContentType返回类型 _request.ContentType = item.ContentType; //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息 _request.UserAgent = item.UserAgent; // 编码 _encoding = item.Encoding; //设置安全凭证 _request.Credentials = item.Credentials; //设置Cookie SetCookie(item); //来源地址 _request.Referer = item.Referer; //是否执行跳转功能 _request.AllowAutoRedirect = item.Allowautoredirect; if (item.MaximumAutomaticRedirections > 0) { _request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections; } //设置Post数据 SetPostData(item); //设置最大连接 if (item.Connectionlimit > 0) { _request.ServicePoint.ConnectionLimit = item.Connectionlimit; } }
private void InitializeStation() { string urlStation = StringHelper.GetConfigValByKey(Constant.StationNameUrl, false); string urlInitial = StringHelper.GetConfigValByKey(Constant.InitialUrl, false); string urlAuth = StringHelper.GetConfigValByKey(Constant.Auth, false); //LogHelper.WriteTextLog("Loaded", url); LogHelper.Info(urlStation); Station station = null; String[] stations = null; try { //stations = client.HttpGet(url, String.Empty).ToString().Split('@'); HttpItem item = new HttpItem() { URL = urlStation }; stations = HttpRequest.HttpGet(item).ToString().Split('@'); //初始化生成客户端环境Cookie HttpItem itemInit = new HttpItem() { URL = urlInitial, Referer = @"http://www.12306.cn/mormhweb/", IsInitialLoaded = true }; HttpRequest.HttpGet(itemInit); HttpItem item2 = new HttpItem() { URL = urlAuth, Referer = @"https://kyfw.12306.cn/otn/login/init", PostData = new Dictionary <string, string>() { { "appid", "otn" } }, IsInitialLoaded = true }; HttpRequest.HttpPost(item2); } catch (Exception ex) { MessageBoxResult dialog = MessageBox.Show("请排查以下可能故障:\r\n1.请检查网络故障 \r\n2.请确认12306官网是否可以登录\r\n3." + ex.Message + "4.关闭程序重新打开", "出错了", MessageBoxButton.OKCancel, MessageBoxImage.Error); if (dialog == MessageBoxResult.OK) { Task.Factory.StartNew(InitializeStation); LogHelper.Error(ex.Message); return; } else { Environment.Exit(0); } } UpdateProgressBarDelegate updatePbDelegate = this.InitialProgressBar(stations.Length); var i = 0; //item = "sha|上海|SHH|shanghai|sh|10" foreach (var item in stations) { if (item.Contains("station_names")) { continue; } if (!String.IsNullOrEmpty(item)) { var items = item.Split('|'); station = new Station(items[0], items[1], items[2], items[3], items[4], items[5]); SystemCache.SetTrainStation(item.Split('|')[1], station); } i++; this.UpdateUIInfo(updatePbDelegate, i, station); } Dispatcher.Invoke(() => { this.Hide(); new Login().Show(); string msg = String.Format("加载完毕,共{0}个车站。", stations.Length); LogHelper.Auth(msg); LogHelper.Info(msg); LogHelper.Log(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, msg); }); }
/// <summary> /// 获取数据的并解析的方法 /// </summary> /// <param name="item"></param> /// <param name="result"></param> private void GetData(HttpItem item, HttpResult result) { if (response == null) { return; } #region base //获取StatusCode result.StatusCode = response.StatusCode; //获取最后访问的URl result.ResponseUri = response.ResponseUri.ToString(); //获取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 }
private void FiddlerApplicationAfterSessionComplete(Session oSession) { if (oSession.oRequest.headers == null) { return; } var httpitem = new HttpItem { Parameters = oSession.oRequest.headers.ToString() }; if ((oSession.BitFlags & SessionFlags.IsHTTPS) != 0) { httpitem.URL = "https://" + oSession.url; } else { httpitem.URL = "http://" + oSession.url; } if (oSession.RequestMethod.ToLower() == "post") { httpitem.Method = MethodType.POST; } httpitem.Postdata = Encoding.Default.GetString(oSession.RequestBody); if (string.IsNullOrWhiteSpace(SelectText) == false) { var content = oSession.GetResponseBodyAsString(); content = JavaScriptAnalyzer.Decode(content); if (content.Contains(SelectText) == false) { return; } } StopVisit(); httpitem.DictCopyTo(Http); var post = ""; if (Http.Method == MethodType.POST) { post = "post请求的内容为:\n" + httpitem.Postdata + "\n"; } var window = MainFrm as Window; ControlExtended.UIInvoke(() => { if (window != null) { window.Topmost = true; } }); var info = $"已经成功获取嗅探字段! 真实请求地址:\n{oSession.url},\n已自动配置了网页采集器,请求类型为{Http.Method}\n {post}已经刷新了网页采集器的内容"; XLogSys.Print.Info(info); IsSuperMode = true; ControlExtended.UIInvoke(() => { if (window != null) { window.Topmost = false; } }); SniffSucceed?.Invoke(this, new EventArgs()); URL = oSession.url; }
/// <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), RegexString.Enconding, 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); } } } }
public SmartCrawler() { Http = new HttpItem(); CrawlItems = new ObservableCollection <CrawlItem>(); helper = new HttpHelper(); URL = ""; HtmlDoc = new HtmlDocument(); SelectText = ""; IsMultiData = ScriptWorkMode.List; IsAttribute = true; URL = "www.cnblogs.com"; ShareCookie = new TextEditSelector(); ShareCookie.GetItems = AppHelper.GetAllCrawlerNames(null); Commands2 = CommandBuilder.GetCommands( this, new[] { new Command("添加", obj => AddNewItem(), obj => string.IsNullOrEmpty(SelectName) == false && string.IsNullOrEmpty(SelectXPath) == false, "add"), new Command("搜索", obj => GetXPathAsync(), obj => currentXPaths != null, "magnify"), new Command("手气不错", obj => FeelLucky(), obj => IsMultiData != ScriptWorkMode.NoTransform && isBusy == false, "smiley_happy" ), new Command("提取测试", obj => { if (!(CrawlItems.Count > 0).SafeCheck("属性数量不能为空")) { return; } if (IsMultiData == ScriptWorkMode.List && CrawlItems.Count < 2) { MessageBox.Show("列表模式下,属性数量不能少于2个", "提示信息"); return; } if (string.IsNullOrEmpty(this.URLHTML)) { this.VisitUrlAsync(); } var datas = HtmlDoc.DocumentNode.GetDataFromXPath(CrawlItems, IsMultiData, RootXPath, RootFormat) .ToList(); var view = PluginProvider.GetObjectInstance <IDataViewer>("可编辑列表"); var r = view.SetCurrentView(datas); ControlExtended.DockableManager.AddDockAbleContent( FrmState.Custom, r, "提取数据测试结果"); var rootPath = XPath.GetMaxCompareXPath(CrawlItems.Select(d => d.XPath)); if (datas.Count > 1 && string.IsNullOrEmpty(RootXPath) && rootPath.Length > 0 && IsMultiData == ScriptWorkMode.List && MessageBox.Show($"检测到列表的根节点为:{rootPath},是否设置根节点路径? 此操作有建议有经验用户使用,小白用户请点【否】", "提示信息", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { RootXPath = rootPath; RootFormat = SelectorFormat.XPath; HtmlDoc.CompileCrawItems(CrawlItems); OnPropertyChanged("RootXPath"); } }, icon: "page_search") }); }
/// <summary> /// 为请求准备参数 /// </summary> ///<param name="item">参数列表</param> private void SetRequest(HttpItem item) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 //设置HTTPS方式 if (item.URL.ToLower().Contains("https://")) ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(new Uri(item.URL)); // 验证证书 SetCer(item); SetCerList(item); //设置Header参数 if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys) { request.Headers.Add(key, item.Header[key]); } // 设置代理 SetProxy(item); if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion; request.ServicePoint.Expect100Continue = item.Expect100Continue; //请求方式Get或者Post request.Method = item.Method; request.Timeout = item.Timeout; request.KeepAlive = item.KeepAlive; request.ReadWriteTimeout = item.ReadWriteTimeout; if (!string.IsNullOrWhiteSpace(item.Host)) { request.Host = item.Host; } if (item.IfModifiedSince != null) request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince); //Accept request.Accept = item.Accept; //ContentType返回类型 request.ContentType = item.ContentType; //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息 request.UserAgent = item.UserAgent; // 编码 encoding = item.Encoding; //设置安全凭证 request.Credentials = item.ICredentials; //设置Cookie SetCookie(item); //来源地址 request.Referer = item.Referer; //是否执行跳转功能 request.AllowAutoRedirect = item.Allowautoredirect; if (item.MaximumAutomaticRedirections > 0) { request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections; } //设置Post数据 SetPostData(item); //设置最大连接 if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit; }
public HttpResult GetHtml(HttpItem item) { HttpResult httpResult = new HttpResult(); try { this.SetRequest(item); } catch (Exception ex) { HttpResult httpResult2 = new HttpResult(); httpResult2.Cookie = string.Empty; httpResult2.Header = null; httpResult2.Html = ex.Message; httpResult2.StatusDescription = "配置参数时出错:" + ex.Message; return(httpResult2); } try { using (this.response = (HttpWebResponse)this.request.GetResponse()) { httpResult.StatusCode = this.response.StatusCode; httpResult.StatusDescription = this.response.StatusDescription; httpResult.Header = this.response.Headers; if (this.response.Cookies != null) { httpResult.CookieCollection = this.response.Cookies; } if (((NameValueCollection)this.response.Headers)["set-cookie"] != null) { httpResult.Cookie = ((NameValueCollection)this.response.Headers)["set-cookie"]; } byte[] array = null; using (MemoryStream memoryStream = new MemoryStream()) { if (this.response.ContentEncoding != null && this.response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { new GZipStream(this.response.GetResponseStream(), CompressionMode.Decompress).CopyTo(memoryStream, 10240); } else { this.response.GetResponseStream().CopyTo(memoryStream, 10240); } array = memoryStream.ToArray(); } if (array != null & array.Length > 0) { if (item.ResultType == ResultType.Byte) { httpResult.ResultByte = array; } if (this.encoding == null) { Match match = Regex.Match(Encoding.Default.GetString(array), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase); string text = (match.Groups.Count > 1) ? match.Groups[2].Value.ToLower() : string.Empty; if (text.Length > 2) { this.encoding = Encoding.GetEncoding(text.Trim().Replace("\"", "").Replace("'", "") .Replace(";", "") .Replace("iso-8859-1", "utf-8")); } else if (string.IsNullOrWhiteSpace(this.response.CharacterSet)) { this.encoding = Encoding.UTF8; } else { this.encoding = Encoding.GetEncoding(this.response.CharacterSet); } } httpResult.Html = this.encoding.GetString(array); } else { httpResult.Html = "本次请求并未返回任何数据"; } } } catch (WebException ex2) { this.response = (HttpWebResponse)ex2.Response; httpResult.Html = ex2.Message; if (this.response != null) { httpResult.StatusCode = this.response.StatusCode; httpResult.StatusDescription = this.response.StatusDescription; } } catch (Exception ex) { httpResult.Html = ex.Message; } if (item.IsToLower) { httpResult.Html = httpResult.Html.ToLower(); } return(httpResult); }
/// <summary> /// 根据相传入的数据,得到相应页面数据 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> internal HttpResult GetHtml(HttpItem item) { //返回参数 HttpResult result = new HttpResult(); try { //准备参数 SetRequest(item); } catch (Exception ex) { //配置参数时出错 return new HttpResult { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message }; } try { //请求数据 using (response = (HttpWebResponse)request.GetResponse()) { foreach (Cookie cookie in response.Cookies) { item.CookieCollection.Add(cookie); } 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">参数列表</param> private void SetRequest(HttpItem item) { // 验证证书 SetCer(item); if (item.IpEndPoint != null) { _ipEndPoint = item.IpEndPoint; //设置本地的出口ip和端口 _request.ServicePoint.BindIPEndPointDelegate = BindIpEndPointCallback; } //设置Header参数 if (item.Header != null && item.Header.Count > 0) { foreach (var key in item.Header.AllKeys) { _request.Headers.Add(key, item.Header[key]); } } // 设置代理 SetProxy(item); if (item.ProtocolVersion != null) { _request.ProtocolVersion = item.ProtocolVersion; } _request.ServicePoint.Expect100Continue = item.Expect100Continue; //请求方式Get或者Post _request.Method = item.Method; _request.Timeout = item.Timeout; _request.KeepAlive = item.KeepAlive; _request.ReadWriteTimeout = item.ReadWriteTimeout; if (!string.IsNullOrWhiteSpace(item.Host)) { _request.Host = item.Host; } if (item.IfModifiedSince != null) { _request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince); } //Accept _request.Accept = item.Accept; //ContentType返回类型 _request.ContentType = item.ContentType; //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息 _request.UserAgent = item.UserAgent; // 编码 _encoding = item.Encoding; //设置安全凭证 _request.Credentials = item.Credentials; //设置Cookie SetCookie(item); //来源地址 _request.Referer = item.Referer; //是否执行跳转功能 _request.AllowAutoRedirect = item.Allowautoredirect; if (item.MaximumAutomaticRedirections > 0) { _request.MaximumAutomaticRedirections = item.MaximumAutomaticRedirections; } //设置Post数据 SetPostData(item); //设置最大连接 if (item.Connectionlimit > 0) { _request.ServicePoint.ConnectionLimit = item.Connectionlimit; } }
/// <summary> /// 根据相传入的数据,得到相应页面数据 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> public HttpResult GetHtml(HttpItem item) { //返回参数 HttpResult result = new HttpResult(); try { //准备参数 SetRequest(item); } catch (Exception ex) { return new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message }; } try { #region 得到请求的response using (response = (HttpWebResponse)request.GetResponse()) { result.StatusCode = response.StatusCode; result.StatusDescription = response.StatusDescription; result.Header = response.Headers; if (response.Cookies != null) result.CookieCollection = response.Cookies; if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"]; byte[] ResponseByte = null; using (MemoryStream _stream = new MemoryStream()) { //GZIIP处理 if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { //开始读取流并设置编码方式 new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240); } else { //开始读取流并设置编码方式 response.GetResponseStream().CopyTo(_stream, 10240); } //获取Byte ResponseByte = _stream.ToArray(); } if (ResponseByte != null & ResponseByte.Length > 0) { //是否返回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 = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower().Trim() : string.Empty; if (c.Length > 2) { try { if (c.IndexOf(" ") > 0) c = c.Substring(0, c.IndexOf(" ")); encoding = Encoding.GetEncoding(c.Replace("\"", "").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); } } //得到返回的HTML result.Html = encoding.GetString(ResponseByte); } else { //得到返回的HTML result.Html = "本次请求并未返回任何数据"; } } #endregion } catch (WebException ex) { //这里是在发生异常时返回的错误信息 response = (HttpWebResponse)ex.Response; result.Html = ex.Message; if (response != null) { result.StatusCode = response.StatusCode; result.StatusDescription = response.StatusDescription; } } catch (Exception ex) { result.Html = ex.Message; } if (item.IsToLower) result.Html = result.Html.ToLower(); return result; }
/// <summary> /// 根据相传入的数据,得到相应页面数据 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> public HttpResult GetHtml(HttpItem item) { //返回参数 _result = new HttpResult(); try { //准备参数 SetRequest(item); } catch (Exception ex) { //配置参数时出错 return(new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message }); } 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(); } //重置request,response为空 if (!item.IsReset) { return(_result); } _request = null; _response = null; return(_result); }
/// <summary> /// 设置多个证书 /// </summary> /// <param name="item"></param> private void SetCerList(HttpItem item) { if (item.ClentCertificates != null && item.ClentCertificates.Count > 0) { foreach (X509Certificate c in item.ClentCertificates) { request.ClientCertificates.Add(c); } } }
protected void Page_Load(object sender, EventArgs e) { ////创建Httphelper对象 HttpHelper http = new HttpHelper(); //创建Httphelper参数对象 HttpItem item = new HttpItem() { URL = "http://www.sufeinet.com", //URL 必需项 Method = "get", //URL 可选项 默认为Get ContentType = "text/html", //返回类型 可选项有默认值 Expect100Continue = false //ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值 }; //请求的返回值对象 HttpResult result = http.GetHtml(item); //获取请请求的Html //string html = result.Html; //List<AItem> alist = HttpHelper.GetAList(html); ////创建Httphelper对象 //HttpHelper http = new HttpHelper(); ////创建Httphelper参数对象 //HttpItem item = new HttpItem() //{ // URL = "http://www.sufeinet.com",//URL 必需项 // Method = "get",//URL 可选项 默认为Get // ContentType = "text/html",//返回类型 可选项有默认值 // //ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值 //}; ////请求的返回值对象 //HttpResult result = http.GetHtml(item); ////获取请请求的Html //string html = result.Html; //List<ImgItem> imglist = HttpHelper.GetImgList(html); ////创建Httphelper对象 //HttpHelper http = new HttpHelper(); ////创建Httphelper参数对象 //HttpItem item = new HttpItem() //{ // URL = "http://www.sufeinet.com",//URL 必需项 // Method = "get",//URL 可选项 默认为Get // ContentType = "text/html",//返回类型 可选项有默认值 // //ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值 //}; ////请求的返回值对象 //HttpResult result = http.GetHtml(item); ////获取请请求的Html //string html = result.Html; //html = HttpHelper.ReplaceNewLine(html); //html = HttpHelper.StripHTML(html); string html = "苏飞论坛"; string str = HttpHelper.GetBetweenHtml(html, "苏", "论坛"); byte[] b = HttpHelper.StringToByte("苏飞"); //可指定编码 b = HttpHelper.StringToByte("苏飞", Encoding.UTF8); string s = HttpHelper.ByteToString(b); s = HttpHelper.ByteToString(b, Encoding.UTF8); string title = HttpHelper.GetHtmlTitle(html); }
/// <summary> /// 设置Post数据 /// </summary> /// <param name="item">Http参数</param> private void SetPostData(HttpItem item) { //验证在得到结果时是否有传入数据 if (request.Method.Trim().ToLower().Contains("post")) { if (item.PostEncoding != null) { postencoding = item.PostEncoding; } byte[] buffer = null; //写入Byte类型 if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0) { //验证在得到结果时是否有传入数据 buffer = item.PostdataByte; }//写入文件 else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrWhiteSpace(item.Postdata)) { StreamReader r = new StreamReader(item.Postdata, postencoding); buffer = postencoding.GetBytes(r.ReadToEnd()); r.Close(); } //写入字符串 else if (!string.IsNullOrWhiteSpace(item.Postdata)) { buffer = postencoding.GetBytes(item.Postdata); } if (buffer != null) { request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); } } }
/// <summary> /// Share link /// </summary> /// <param name="fs_id">fs_id</param> /// <param name="password">Password if need</param> /// <returns>DBCFileShareStruct</returns> public static Task <DBCFileShareStruct> Share(ulong fs_id, string password = "") { return(Task.Factory.StartNew(() => { if (!CheckCookie()) { LoadCookie(Setting.Baidu_CookiePath); } if (bdstoken == null || bdstoken.Equals("")) { if (!GetParamFromHtml()) { throw new Exception("Get param from html error."); } } StringBuilder SB = new StringBuilder(); SB.Append(string.Format("fid_list=[{0}]&schannel=", fs_id)); if (password.Equals("")) { // No password SB.Append("0&channel_list=[]"); } else if (password.Count() == 4) { // Set password SB.Append(string.Format("4&channel_list=[]&pwd={0}", password)); } else { throw new Exception("Password error, set 4 characters."); } HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = string.Format("{0}set?channel=chunlei&web=1&app_id=250528&clienttype=0&bdstoken={1}", BDCShareURL, bdstoken), Method = "POST", Encoding = Encoding.UTF8, Timeout = BDC.Timeout, Referer = "http://pan.baidu.com/disk/home#list/vmode=list&path=%2F", Host = "pan.baidu.com", Cookie = Cookies, Postdata = SB.ToString(), PostEncoding = Encoding.UTF8, ContentType = "application/x-www-form-urlencoded; charset=UTF-8" }; string result = http.GetHtml(item).Html; if (result.Contains("errno\":0")) { var json = (JObject)JsonConvert.DeserializeObject(result); return new DBCFileShareStruct { ctime = Convert.ToUInt32(json["ctime"]), shareid = Convert.ToUInt64(json["shareid"]), link = json["link"].ToString(), shorturl = json["shorturl"].ToString(), password = password }; } else { throw new ErrorCodeException(); } })); }
/// <summary> /// 为请求准备参数 /// </summary> ///<param name="item">参数列表</param> private void SetRequest(HttpItem item) { // 验证证书 SetCer(item); //设置Header参数 if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys) { request.Headers.Add(key, item.Header[key]); } // 设置代理 SetProxy(item); if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion; request.ServicePoint.Expect100Continue = item.Expect100Continue; //请求方式Get或者Post request.Method = item.Method; request.Timeout = item.Timeout; request.KeepAlive = item.KeepAlive; request.ReadWriteTimeout = item.ReadWriteTimeout; if (!string.IsNullOrWhiteSpace(item.Host)) { request.Host = item.Host; } //Accept request.Accept = item.Accept; //ContentType返回类型 request.ContentType = item.ContentType; //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息 request.UserAgent = item.UserAgent; // 编码 encoding = item.Encoding; //设置Cookie SetCookie(item); //来源地址 request.Referer = item.Referer; //是否执行跳转功能 request.AllowAutoRedirect = item.Allowautoredirect; //设置Post数据 SetPostData(item); //设置最大连接 if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit; }
/// <summary> /// Move file to toFolder and get download URL /// </summary> /// <param name="shareLink">Share link</param> /// <param name="toFolder">Remote path</param> /// <returns>Download URL</returns> public static Task <string> Transfer(string shareLink, string toFolder = "/apps/wp2pcs") { return(Task.Factory.StartNew(() => { if (!CheckCookie()) { LoadCookie(Setting.Baidu_CookiePath); } if (bdstoken == null || bdstoken.Equals("")) { if (!GetParamFromHtml()) { throw new Exception("Get param from html error."); } } HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = shareLink, Timeout = BDC.Timeout, Referer = shareLink, Host = "pan.baidu.com", Accept = "*/*", Cookie = Cookies }; string result = http.GetHtml(item).Html; //string cookie = http.GetHtml(item).Cookie; Match match_ShareId = Regex.Match(result, "shareid\":[0-9]{1,}(?=,)"); if (!match_ShareId.Success) { throw new Exception("Can not match shareid."); } ulong shareid = Convert.ToUInt64(match_ShareId.Value.Replace("shareid\":", "")); Match match_FromUserId = Regex.Match(result, "/share/home\\?uk=[0-9]{1,}(?=\")"); if (!match_FromUserId.Success) { throw new Exception("Can not match fromuserid."); } ulong fromuserid = Convert.ToUInt64(match_FromUserId.Value.Replace("/share/home?uk=", "")); Match match_File = Regex.Match(result, "server_filename\":\".*?(?=\",\")"); if (!match_File.Success) { throw new Exception("Can not match file name."); } string file = match_File.Value.Replace("server_filename\":\"", ""); Match Match_ParentPath = Regex.Match(result, "parent_path\":\".*?(?=\",\")"); if (!Match_ParentPath.Success) { throw new Exception("Can not match file parent path."); } string parentPath = Match_ParentPath.Value; if (parentPath.Equals("parent_path\":\"")) { parentPath = "%2F"; } else { parentPath = parentPath.Replace("parent_path\":\"", "") + "%2F"; } StringBuilder SB = new StringBuilder(); //SB.Append("filelist=[\"/"); //SB.Append(ConvertFileName(file)); SB.Append("filelist=[\""); SB.Append(parentPath); SB.Append(Other.Tools.URLEncoding(Regex.Unescape(file), Encoding.UTF8)); SB.Append("\"]&path="); //SB.Append(toFolder); SB.Append(Other.Tools.URLEncoding(toFolder, Encoding.UTF8)); HttpItem item_Transfer = new HttpItem() { URL = string.Format("{0}transfer?shareid={1}&from={2}&ondup=newcopy&async=1&bdstoken={3}&channel=chunlei&clienttype=0&web=1&app_id=250528", BDCShareURL, shareid, fromuserid, bdstoken), Method = "POST", Encoding = Encoding.UTF8, Timeout = BDC.Timeout, Referer = shareLink, Host = "pan.baidu.com", Cookie = Cookies, Postdata = SB.ToString(), PostEncoding = Encoding.UTF8, ContentType = "application/x-www-form-urlencoded; charset=UTF-8", Accept = "*/*", ProtocolVersion = new Version(1, 1), KeepAlive = true }; string result_Transfer = http.GetHtml(item_Transfer).Html; if (result_Transfer.Contains("errno\":0")) { return string.Format("{0}/{1}", toFolder, Regex.Unescape(file)); } throw new ErrorCodeException("BDC.Transfer"); })); }
public string Excute(WeChatConfig config, WeChatData data) { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem(); item.URL = string.Format("{0}menu/get?access_token={1}", config.APIUrl, config.AccessToken); item.Method = "GET"; item.ContentType = "application/x-www-form-urlencoded"; string result = http.GetHtml(item); return result; }
/// <summary> /// 根据Url获取图片 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回图片</returns> internal Image GetImage(HttpItem item) { item.ResultType = ResultType.Byte; return(ImageHelper.ByteToImage(GetHtml(item).ResultByte)); }
/// <summary> /// 快速Post数据这个访求与GetHtml一样,只是不接收返回数据,只做提交。 /// </summary> /// <param name="item">参数类对象</param> /// <returns>返回HttpResult类型</returns> internal HttpResult FastRequest(HttpItem item) { return(httpbase.FastRequest(item)); }
public HttpResult GetHttpResult(HttpItem item) { return(new HttpHelper().GetHtml(item)); }
public void GetMinFreeShipping(Site site) { logInfo logInfo = new logInfo(); logInfo.Location = this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name; //get the minimun purchase amount for free shipping from it's F&Q page string url = "https://jet.com/help-center/shipping-and-returns"; HttpItem httpItem = new HttpItem(); httpItem.Cookie = string.Empty; httpItem.Method = "GET"; httpItem.Header = null; httpItem.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; httpItem.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36"; //httpItem.Timeout = 60000 * 5; httpItem.URL = url; HttpHelper httpHelper = new HttpHelper(); HttpResult httpResult = null; try { httpResult = httpHelper.GetHtml(httpItem); if (httpResult == null) { logInfo.KeyInfo = "Remote HTML is null (timeout etc.)"; Log4Helper.WarningLog(logInfo); } else { if (httpResult.StatusCode != System.Net.HttpStatusCode.OK) { logInfo.KeyInfo = "Remote HTML is null (timeout etc.)"; Log4Helper.WarningLog(logInfo); } else { string content = httpResult.Html; if (content.Length < 200) { return; } HtmlHelper html = new HtmlHelper(content); var fq_node = html.GetNodeByXPath("//div[@class='page_content']/h1"); var ans = html.GetNodesByXPath("following-sibling::p", fq_node); Regex minfreeshipping_pattern = new Regex(@"orders\sover\s\$(\d+)"); Regex shippingfee_pattern = new Regex(@"fixed\sshipping\sfee\sof\s\$(\d+(\.\d+)?)"); int match_min = 0; int match_fee = 0; foreach(HtmlNode a in ans) { if(minfreeshipping_pattern.Match(a.InnerText).Success) { min_shipping = Convert.ToSingle(minfreeshipping_pattern.Match(a.InnerText).Groups[1].Value); match_min = 1; } if (shippingfee_pattern.Match(a.InnerText).Success) { fee_shipping = Convert.ToSingle(shippingfee_pattern.Match(a.InnerText).Groups[1].Value); match_fee = 1; } if(match_min==1&&match_fee==1) { continue; } } } } } catch (Exception ex) { logInfo.KeyInfo = "Could not get remote HTML"; Log4Helper.ErrorLog(logInfo); } }
public static string Do(string userName = "******", string password = "") { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem { URL = "https://ac.ppdai.com/User/Login",//URL 必需项 Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36", Header = new WebHeaderCollection { {"Accept-Encoding"," gzip, deflate, sdch"}, {"Accept-Language","zh-CN,zh;q=0.8"} } }; HttpResult result = http.GetHtml(item); string cookie = result.Cookie; Dictionary<string, string> cks = new Dictionary<string, string>(); foreach (string source in cookie.Split(';').Where(c => !string.IsNullOrEmpty(c))) { if (source.Split('=').Count() > 2) { foreach (string ssource in source.Split(',')) { string[] ss = ssource.Split('='); string key = ss[0].Trim().Split(',').Last(); if (ss.Count() == 2 && !cks.ContainsKey(key.Trim())) { cks.Add(key.Trim(), ss[1].Trim()); } } } else { string[] ss = source.Split('='); string key = ss[0].Trim().Split(',').Last(); if (ss.Count() == 2 && !cks.ContainsKey(key.Trim())) { cks.Add(key.Trim(), ss[1].Trim()); } } } string uniqueid = cks["uniqueid"]; string dtNow = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); cookie = string.Format( "uniqueid={0}; regSourceId=0; referID=0; fromUrl=; referDate={1}; currentUrl=https%3A%2F%2Fac.ppdai.com%2Fuser%2Flogin", uniqueid, HttpUtility.UrlEncode(dtNow)); item = item.GetNextHttpItem("https://ac.ppdai.com/User/Login", cookie); item.Header.Add("X-Requested-With: XMLHttpRequest"); item.ContentType = "application/x-www-form-urlencoded"; item.Referer = "https://ac.ppdai.com/User/Login"; item.Method = "POST"; item.Postdata = string.Format("IsAsync=true&Redirect=&UserName={0}&Password={1}&RememberMe=false", userName, password); result = http.GetHtml(item); cookie = result.Cookie; foreach (string source in cookie.Split(';').Where(c => !string.IsNullOrEmpty(c))) { if (source.Split('=').Count() > 2) { foreach (string ssource in source.Split(',')) { string[] ss = ssource.Split('='); string key = ss[0].Trim().Split(',').Last(); if (ss.Count() == 2 && !cks.ContainsKey(key.Trim())) { cks.Add(key.Trim(), ss[1].Trim()); } } } else { string[] ss = source.Split('='); string key = ss[0].Trim().Split(',').Last(); if (ss.Count() == 2 && !cks.ContainsKey(key.Trim())) { cks.Add(key.Trim(), ss[1].Trim()); } } } cookie = string.Join(";", cks.Select(c => c.Key + "=" + c.Value)); item = item.GetNextHttpItem("http://invest.ppdai.com/PaiMoney/PaiMoneySignin", cookie); item.Accept = "*"; item.ContentType = ""; item.Referer = "http://invest.ppdai.com/account/lend"; item.Postdata = ""; result = http.GetHtml(item); return result.Html; }