using NaiBa.Util.Extension; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; namespace AllBase { public class GetProxyHtml { private static string headerKey = "Authorization"; public static string ProxyIpKey = "proxy_ip_pingduoduo"; public static WebProxy proxyObject; public static List ips = new List(); public static string txtUrl = "";// "http://125.88.158.218:1800/ip/list/line/test001"; public static string orderId = ""; public static int loopCount = 15; public static int jinbaoCount = 8; public static int ipCount = 8; public static int oldindex = -1; public static List UserAgentList = new List(); public static List LastIpList { get; set; } // 上一次获取的所有IP,用来判断本次获取是否是重复的 public static int IpIndex { get; set; } public static string[] IpList { get; set; } private static bool IsInit() { if (IpList == null || IpList.Length == 0 || IpIndex > IpList.Length - 1) { return true; } else { return false; } } public static DateTime lastGetTime = DateTime.Now; public static void Init() // 火箭ip获取 { while (lastGetTime.AddSeconds(4) > DateTime.Now) { Thread.Sleep(50 + Randomx.GetId(100)); } lastGetTime = DateTime.Now; IpIndex = 0; string proxyIpHtml = UploadTaobaoPost(txtUrl, "", true, "", false, "");//http://125.88.158.218:1800/ip/list/test/4185_158905271 http://125.88.158.218:1800/ip/list/line/test001 int tryTimes = 0; int maxTryTimes = 10; while (true && tryTimes <= maxTryTimes) { if (!string.IsNullOrEmpty(proxyIpHtml)) { ips = proxyIpHtml.Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList(); if (LastIpList == null || LastIpList.Count == 0) { break; } else { // 确保本次获取的ip和上一次的不一样 if (LastIpList.Where(p => ips.Contains(p)).Count() != LastIpList.Count) { break; } } } if (proxyIpHtml.Contains("远程服务器返回错误: (888)")) break; tryTimes++; Thread.Sleep(1000); } CloseIp(LastIpList); if (ips != null) { LastIpList = ips; IpList = ips.ToArray(); } } public static WebProxy GetIPSProxyQueue() { if (string.IsNullOrEmpty(txtUrl)) return null; int AgainCount = 0; Again: if (IsInit()) { Init(); } if (IpList.Length == 0) { if (AgainCount < 2) { AgainCount++; Thread.Sleep(50); goto Again; } } WebProxy webProxy = null; if (IpList.Length == 0) return webProxy; string ip = IpList[IpIndex]; string[] ipArr = ip.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); switch (ipArr.Length) { case 2: webProxy = new WebProxy(ipArr[0], ipArr[1].ParseToInt()); break; case 4: webProxy = new WebProxy(ipArr[0], ipArr[1].ParseToInt()); webProxy.Credentials = new NetworkCredential(ipArr[2], ipArr[3]); break; } return webProxy; } /// /// 获取火箭IP /// /// /// public static WebProxy GetIPSProxyQueue(out string message) { message = ""; if (string.IsNullOrEmpty(txtUrl)) return null; int AgainCount = 0; Again: if (IsInit()) { Init(); } if (IpList.Length == 0) { if (AgainCount < 1) { AgainCount++; Thread.Sleep(50); goto Again; } } WebProxy webProxy = null; if (IpList.Length == 0) return webProxy; string ip = IpList[IpIndex]; string[] ipArr = ip.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); switch (ipArr.Length) { case 2: webProxy = new WebProxy(ipArr[0], ipArr[1].ParseToInt()); break; case 4: webProxy = new WebProxy(ipArr[0], ipArr[1].ParseToInt()); webProxy.Credentials = new NetworkCredential(ipArr[2], ipArr[3]); break; } if (ip.Contains("远程服务器返回错误: (888)")) message = "888"; return webProxy; } public static void GetIPS() { if (string.IsNullOrEmpty(txtUrl)) return; string htmlx = UploadTaobaoPost(txtUrl, "", true, "", false, ""); List striparr = htmlx.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList(); striparr = striparr.Where(s => !string.IsNullOrEmpty(s)).ToList(); if (striparr.Count < 5) return; ips = striparr; } public static void GetIPSProxy() { if (string.IsNullOrEmpty(txtUrl)) return; string htmlx = UploadTaobaoPost(txtUrl, "", true, "", false, ""); List striparr = htmlx.Split('\n').ToList(); striparr = striparr.Where(s => !string.IsNullOrEmpty(s)).ToList(); if (striparr.Count < 5) return; ips = striparr; } public static List GetIPSProxyObject() { if (string.IsNullOrEmpty(txtUrl)) return null; string htmlx = UploadTaobaoPost(txtUrl, "", true, "", false, ""); List striparr = htmlx.Split('\n').ToList(); striparr = striparr.Where(s => !string.IsNullOrEmpty(s)).ToList(); if (striparr.Count < 5) return null; return striparr; } public static string RequestUrl_TaoBao_com(long lProdId, string uri, string[] UserAgentListout, out bool IsOverdue) { IsOverdue = false; int count = 0; try { Again: count++; HttpWebResponse response = null; try { //string UserAgentTemp = UserAgentList[new Random().Next(UserAgentList.Count())]; //if (!string.IsNullOrEmpty(UserAgentTemp) && !UserAgentTemp.Contains("MicroMessenger")) //{ // UserAgentTemp = UserAgentTemp.Insert(new Random().Next(UserAgentTemp.Length), "MicroMessenger" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, new Random().Next(5))); //} //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://goods.66daili.cn/api/goods/taobao-api/?orderid=" + GetProxyHtml.orderId + "&token=5AF433ED506FA475&goodID=" + lProdId); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ecdata-api.66daili.cn/api/ecdata/taobao/detail/?orderid=" + GetProxyHtml.orderId + "&token=5AF433ED506FA475&itemID=" + lProdId); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://ecdata-api.66daili.cn/ecdata/api/detail/?orderid={orderId}&token=5AF433ED506FA475&itemID={lProdId}"); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://ec-private.66daili.cn/api/private/detail/?orderid={orderId}&token=5AF433ED506FA475&item_id={lProdId}"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://ecdata-api.66daili.cn/api/common/vip/get/?orderid={orderId}&token=5AF433ED506FA475&item_id={lProdId}"); request.Timeout = 7000; request.KeepAlive = true; request.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9,en;q=0.8"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"; request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); response = (HttpWebResponse)request.GetResponse(); } catch (Exception ex) { } if (response != null) { string result = ReadResponse(response); result = UnicodeDecode(result); //return result; if (result.Contains("order expire or not exist") || result.Contains("orderid empty")) { IsOverdue = true; return ""; } if (result.Contains("categoryId") || result.Contains("\"dataType\": \"v") || result.Contains("调用成功")) { return UnicodeDecode(result); } if (result.Contains("mtop.taobao.detail.getdesc")) { return result; } else { if (count < 100) { SleepCommon(); goto Again; } } } else { if (count < 100) { SleepCommon(); goto Again; } } return ""; } catch { return ""; } } public static ProxyTypeEnum GetProxyType(string proxyUrl) { ProxyTypeEnum proxyType = ProxyTypeEnum.Unknown; proxyUrl = proxyUrl.ParseToString().Trim(); if (proxyUrl.Contains("125.88.158.218")) { proxyType = ProxyTypeEnum.RocketDaiLi; } else if (proxyUrl.Contains("66daili.cn")) { proxyType = ProxyTypeEnum.SixSixDaiLi; } else if (proxyUrl.Contains("kdlapi.com")) { proxyType = ProxyTypeEnum.KuaiDaiLi; } return proxyType; } private static List listIp = new List(); private static int UseCount = 0; private static readonly object obj = new object(); public static string Request_yangkeduo_com(string sIpAndPort, long lProdId, string[] UserAgentList, out bool IsOverdue, bool IsBreak = false) { IsOverdue = false; int count = 0; try { if (IsBreak) { return ""; } if (UseCount >= 50 && listIp.Count > 0)//代理ip 使用超过150次 才去请求一次ip { listIp.Clear(); } Again: lock (obj) { if (listIp.Count() <= 0) { listIp = InitIp(sIpAndPort, out IsOverdue); UseCount = 0; } } if (IsOverdue) { return ""; } string ProxyIP = listIp[new Random().Next(0, listIp.Count)]; UseCount++; count++; HttpWebResponse response = null; try { string UserAgentTemp = UserAgentList[new Random().Next(UserAgentList.Count())]; if (!string.IsNullOrEmpty(UserAgentTemp) && !UserAgentTemp.Contains("MicroMessenger")) { UserAgentTemp = UserAgentTemp.Insert(new Random().Next(UserAgentTemp.Length), "MicroMessenger" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, new Random().Next(5))); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://mobile.yangkeduo.com/goods.html?goods_id=" + lProdId); request.Timeout = 5000; request.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9,en;q=0.8"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; request.UserAgent = UserAgentTemp; request.Headers.Set(HttpRequestHeader.AcceptEncoding, "deflate"); System.Net.WebProxy proxy = GetWebProxy(ProxyIP); request.Proxy = proxy; response = (HttpWebResponse)request.GetResponse(); } catch (Exception) { } if (response != null) { string result = ReadResponse(response); if (result.Contains("mallID")) { return UnicodeDecode(result); } if (result.Contains("runtime error: index out of range [1] with length 0")) { return "runtime error: index out of range [1] with length 0"; } else { if (count <3) { SleepCommon(); goto Again; } if (count==3) { SleepCommon(); listIp.Clear(); goto Again; } } } else { if (count<3) { SleepCommon(); goto Again; } if (count == 3) { SleepCommon(); listIp.Clear(); goto Again; } } return ""; } catch(Exception ex) { return ""; } } /// /// SHA1字符串加密 /// /// 需要加密字符串 /// 返回40位大写字符串 private static string SHA1(string input) { try { using (System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider()) { string result = ""; byte[] btInput = System.Text.Encoding.UTF8.GetBytes(input); byte[] btOut = sha1.ComputeHash(btInput); for (int i = 0; i < btOut.Length; i++) { result += btOut[i].ToString("x2"); } return result; } } catch { throw; } } /// /// Base64加密 /// /// 加密采用的编码方式 /// 待加密的明文 /// private static string Base64Encode(Encoding encodeType, string source) { string encode = string.Empty; byte[] bytes = encodeType.GetBytes(source); try { encode = Convert.ToBase64String(bytes); } catch { encode = source; } return encode; } public static string RequestUrl_yangkeduo_com(long lProdId, string[] UserAgentListout, out bool IsOverdue) { IsOverdue = false; int count = 0; try { Again: count++; string token = "5AF433ED506FA475"; HttpWebResponse response = null; try { //string UserAgentTemp = UserAgentList[new Random().Next(UserAgentList.Count())]; //if (!string.IsNullOrEmpty(UserAgentTemp) && !UserAgentTemp.Contains("MicroMessenger")) //{ // UserAgentTemp = UserAgentTemp.Insert(new Random().Next(UserAgentTemp.Length), "MicroMessenger" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, new Random().Next(5))); //} //HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://mobile.yangkeduo.com/goods.html?goods_id={lProdId}"); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://ec-api.66daili.cn/api/ecdata/pdd/detail/?orderid={orderId}&token={token}&goodid={lProdId}"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://ec-api.66daili.cn/api/common/async/detail?orderid={orderId}&token={token}&goodid={lProdId}"); request.Timeout = 7000; //request.Proxy = new WebProxy("ytunnel.66daili.cn", 23313); //string sign = SHA1($"{lProdId}|{orderId}|{BaseClass.ProxyToken}|66yitu"); //request.Headers.Add("x-sign", sign); //request.Headers.Add("Proxy-Authorization", "Basic " + Base64Encode(Encoding.UTF8, $"{orderId}:{BaseClass.ProxyToken}")); request.KeepAlive = true; request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0"); request.Headers.Add("Upgrade-Insecure-Requests", @"1"); request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"; request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); request.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9"); response = (HttpWebResponse)request.GetResponse(); } catch (Exception ex) { } if (response != null) { string result = ReadResponse(response); if (result.Contains("order expire or not exist")) { IsOverdue = true; return ""; } if (result.Contains("mall_id") || result.Contains("mallId") || result.Contains("mallID")) { return UnicodeDecode(result); } else { if (count < 50) { SleepCommon(); goto Again; } } } else { if (count < 20) { SleepCommon(); goto Again; } } return ""; } catch { return ""; } } public static void SleepCommon(int number = 100) { Thread.Sleep(number / 2 + Randomx.GetId(number)); } private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; public static string DESDecode(string source, string sKey) { try { byte[] inputByteArray = System.Convert.FromBase64String(source);//Encoding.UTF8.GetBytes(source); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = Keys; System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return str; } } catch (Exception ex) { return ""; } } public static WebProxy GetOne66Ip(string FormName = "") { var IsOverdue = false; lock (obj) { Thread.Sleep(500); if (listIp.Count() <= 0) { Thread.Sleep(3000); listIp = InitIp(GetProxyHtml.txtUrl, out IsOverdue); if (IsOverdue) { LogSave.Add(FormName, "代理Ip过期或套餐不存在"); return null; } } } if (listIp.Count > 0) { string ProxyIP = listIp[0]; listIp.Remove(listIp[0]); return GetWebProxy(ProxyIP); } return null; } /// /// 66代理ip获取 /// /// /// /// public static List InitIp(string ProxyUrl, out bool IsOverdue) { IsOverdue = false; if (listIp.Count > 0) { return listIp; } HttpParam httpParm = new HttpParam { Url = ProxyUrl }; SetAuthorization(httpParm); string proxyIpHtml = string.Empty; List proxyIpList = new List(); int tryTimes = 0; int maxTryTimes = 3; while (true && tryTimes <= maxTryTimes) { proxyIpHtml = GetUtf8(httpParm); if (proxyIpHtml.Contains("过期") || proxyIpHtml.Contains("套餐不存在")) { IsOverdue = true; break; } if (!string.IsNullOrEmpty(proxyIpHtml) && !proxyIpHtml.Contains("后获取") && !proxyIpHtml.Contains("过期") && !proxyIpHtml.Contains("套餐不存在")) { proxyIpList = proxyIpHtml.Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList(); break; } tryTimes++; Thread.Sleep(3000); } return proxyIpList; } public static void SetAuthorization(HttpParam httpParam) { if (httpParam.Header == null) { httpParam.Header = new Dictionary(); } long timeStamp = GetUnixSecondTimeStamp(DateTime.Now) + BaseClass.TimeDifferent; string hexadecimalTimeStamp = timeStamp.ToString("X"); string token = HMACSHA256(hexadecimalTimeStamp, ProxyIpKey).ToUpper(); AddHeader(httpParam, headerKey, string.Format("{0}|{1}", hexadecimalTimeStamp, token)); } public static void AddHeader(HttpParam httpParam, string key, string value) { if (httpParam.Header == null) { httpParam.Header = new Dictionary(); } httpParam.Header.Remove(key); httpParam.Header.Add(key, value); } public static string GetUtf8(HttpParam param) { if (string.IsNullOrEmpty(param.Url)) { return string.Empty; } try { WebClient webClient = GetWebClient(param); byte[] b = webClient.DownloadData(param.Url); return Encoding.UTF8.GetString(b); } catch (Exception ex) { return string.Empty; } } public static WebClientx GetWebClient(HttpParam param) { using (WebClientx webClient = new WebClientx()) { if (param.Proxy != null) { webClient.Proxy = param.Proxy; } if (param.Timeout > 0) { webClient.Timeout = param.Timeout; } else { webClient.Timeout = 60; } if (string.IsNullOrEmpty(param.Accept)) { webClient.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); } else { webClient.Headers.Add("Accept", param.Accept); } if (string.IsNullOrEmpty(param.ContentType)) { webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); } else { webClient.Headers.Add("Content-Type", param.ContentType); } if (string.IsNullOrEmpty(param.AcceptLanguage)) { webClient.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); } else { webClient.Headers.Add("Accept-Language", param.AcceptLanguage); } if (string.IsNullOrEmpty(param.UserAgent)) { webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"); } else { webClient.Headers.Add("User-Agent", param.UserAgent); } if (!string.IsNullOrEmpty(param.Referer)) { webClient.Headers.Add("Referer", param.Referer); } webClient.Headers.Remove("Cookie"); if (!string.IsNullOrEmpty(param.Cookie)) { webClient.Headers.Add("Cookie", param.Cookie); } if (param.Header != null) { foreach (string key in param.Header.Keys) { webClient.Headers.Add(key, param.Header[key]); } } return webClient; } } public static WebProxy GetWebProxy(string ip = "") { WebProxy webProxy = null; try { if (!string.IsNullOrEmpty(ip)) { string[] ipArr = ip.Split(new string[] { " ", ":" }, StringSplitOptions.RemoveEmptyEntries); switch (ipArr.Length) { case 2: webProxy = new WebProxy(ipArr[0], ipArr[1].ParseToInt()); break; case 4: webProxy = new WebProxy(ipArr[0], ipArr[1].ParseToInt()); webProxy.Credentials = new NetworkCredential(ipArr[2], ipArr[3]); break; } return webProxy; } } catch (Exception ex) { } return webProxy; } public static string ReadResponse(HttpWebResponse response, Encoding encoding = null) { using (Stream responseStream = response.GetResponseStream()) { Stream streamToRead = responseStream; if (response.ContentEncoding.ToLower().Contains("gzip")) { streamToRead = new GZipStream(streamToRead, CompressionMode.Decompress); } else if (response.ContentEncoding.ToLower().Contains("deflate")) { streamToRead = new DeflateStream(streamToRead, CompressionMode.Decompress); } if (encoding == null) encoding = Encoding.UTF8; using (StreamReader streamReader = new StreamReader(streamToRead, encoding)) { return streamReader.ReadToEnd(); } } } public static string UnicodeDecode(string html) { html = WebUtility.HtmlDecode(html); Regex reUnicode = new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled); return reUnicode.Replace(html, m => { short c; if (short.TryParse(m.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out c)) { return "" + (char)c; } return m.Value; }); } public static void CloseIp(List ipList) { ProxyTypeEnum proxyType = GetProxyType(txtUrl); if (proxyType == ProxyTypeEnum.RocketDaiLi) { if (ipList != null && ipList.Any()) { try { Uri uri = new Uri(txtUrl); string url = string.Format("{0}://{1}:{2}", uri.Scheme, uri.Host, uri.Port); string userName = uri.Segments[uri.Segments.Length - 1]; string ip = ipList.FirstOrDefault().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)[0]; int[] portArr = ipList.Select(p => p.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)[1].ParseToInt()).ToArray(); string URL = url + "/ip/list/close"; string PostData = JsonConvert.SerializeObject(new { userName = userName, ip = ip, ports = JsonConvert.SerializeObject(portArr) }); //ContentType = "application/json; charset=UTF-8"; string html = UploadTaobaoPost(URL,URL,true, PostData,false,""); } catch { } } } } public static bool CheckProxyStatus(out string message) { bool isProxyAvailable = true; message = ""; try { if (!string.IsNullOrEmpty(txtUrl)) { ProxyTypeEnum proxyType = GetProxyType(txtUrl); if (proxyType == ProxyTypeEnum.RocketDaiLi) { Uri uri = new Uri(txtUrl); string url = string.Format("{0}://{1}:{2}", uri.Scheme, uri.Host, uri.Port); string userName = uri.Segments[uri.Segments.Length - 1]; string Url = url + "/user/info/" + userName; string html = UploadTaobaoPost(Url, "", true, "", false, ""); if (!string.IsNullOrEmpty(html)) { JObject json = JObject.Parse(html); long timeStamp = GetUnixSecondTimeStamp(DateTime.Now) + BaseClass.TimeDifferent; long validity = json["user"]["validity"].ParseToString().ParseToLong(); if (validity > 0 && timeStamp > validity) { isProxyAvailable = false; message = "代理ip已过期"; } else { var proxyObject = GetProxyHtml.GetIPSProxyQueue(out message); if (proxyObject == null) { isProxyAvailable = false; if (message.Contains("888")) message = "当前电脑ip不在代理ip白名单内,请到网页后台白名单管理中设置。"; else message = "未获取到代理ip"; } else { int httpStatus = GetJsonPostStatus(txtUrl); if (httpStatus == 888) { CloseIp(LastIpList); isProxyAvailable = false; message = "当前电脑ip不在代理ip白名单内,请到网页后台白名单管理中设置。"; } } } } } } } catch { } return isProxyAvailable; } public static WebProxy GetNewWebProxy(string ip) { //if (ips.Count == 0) // return null; //Random ra = new Random(Guid.NewGuid().GetHashCode()); //int index = ra.Next(ips.Count); //if (index == oldindex) //{ // index = index + 1; // if (index >= ips.Count) // index = 0; //} //oldindex = index; //string ip = ips[ra.Next(ips.Count)]; if (string.IsNullOrEmpty(ip)) return null; string[] address = Regex.Split(ip, "\\s+", RegexOptions.IgnoreCase); var proxyObject = new WebProxy(address[0], int.Parse(address[1])); string sUserName = address[2]; string sPwd = address[3]; proxyObject.Credentials = new NetworkCredential(sUserName, sPwd); return proxyObject; } public static WebProxy GetWebProxy() { if (ips.Count == 0) return null; Random ra = new Random(Guid.NewGuid().GetHashCode()); string ip = ips[ra.Next(ips.Count)]; if (!ip.Contains(":")) return null; string[] address = ip.Split(':'); proxyObject = new WebProxy(address[0], int.Parse(address[1])); return proxyObject; } public static string UploadNewProxyTaobao(string url, string refere, bool isUtf8, string cookies, WebProxy proxyObject) { return UploadTaobaoNewPost(url, refere, isUtf8, "", false, cookies, proxyObject); } public static string UploadNewProxyTaobaoPost(string url, string refere, bool isUtf8, string data, string cookies, WebProxy proxyObject) { //GetNewWebProxy(); //if (proxyObject == null) // return ""; return UploadTaobaoPost(url, refere, isUtf8, data, false, cookies, proxyObject); } public static string UploadProxyTaobao(string url, string refere, bool isUtf8, string cookies) { GetWebProxy(); if (proxyObject == null) return ""; return UploadTaobaoPost(url, refere, isUtf8, "", false, cookies, proxyObject); } public static string UploadProxyTaobaoPost(string url, string refere, bool isUtf8, string data, string cookies) { GetWebProxy(); if (proxyObject == null) return ""; return UploadTaobaoPost(url, refere, isUtf8, data, false, cookies, proxyObject); } public static string GetHtmlUrl(string url) { try { GetWebProxy(); if (proxyObject == null) return ""; string userAgen = "android Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MLA-AL10 Build/HUAWEIMLA-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 phh_android_version/3.23.0 phh_android_build/228842 phh_android_channel/qihu360"; WebClientx web = new WebClientx(); web.Headers.Add("Accept", "*/*"); web.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); web.Headers.Add("User-Agent", userAgen); web.Headers.Add("Referer", url); string h = web.Headers.ToString(); web.Proxy = proxyObject; byte[] b = web.DownloadData(url); return Encoding.UTF8.GetString(b); } catch (Exception e) { return ""; } } public static string UploadTaobaoPost(string url, string refere, bool isUtf8, string data, bool isPost, string cookies, WebProxy proxyObject) { bool againt = false; try { cookies = GetCookies(url, cookies); if (string.IsNullOrEmpty(cookies)) cookies = BaseClass.ChromeCookies; string pdd = ""; if (!string.IsNullOrEmpty(cookies)) pdd = BaseRegex.GetMatchRegexString(ref cookies, "PDDAccessToken=([^;]+?);"); string pddx = ""; if (!string.IsNullOrEmpty(cookies)) pddx = BaseRegex.GetMatchRegexString(ref cookies, "PASS_ID=([^;]+?);"); Againt: string userAgent = BaseClass.ChromeUserAgent; WebClientx wb = new WebClientx(); wb.Timeout = 3; wb.AllowAutoRedirect = true; wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); wb.Headers.Add("Accept-Encoding", "gzip,deflate"); wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); wb.Headers.Add("User-Agent", userAgent); wb.Proxy = null; if (!string.IsNullOrEmpty(refere)) wb.Headers.Add("Referer", refere); if (!string.IsNullOrEmpty(cookies)) wb.Headers.Add("Cookie", cookies); wb.Proxy = proxyObject; string rehead = wb.Headers.ToString(); var encode = isUtf8 ? Encoding.UTF8 : Encoding.GetEncoding("gbk"); byte[] b, postData = encode.GetBytes(data); string html, head; try { if (string.IsNullOrEmpty(data) && !isPost) { b = wb.DownloadData(url); } else { if (isPost || url.Contains("taobao.com")) { wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); } else { wb.Headers.Add("Content-Type", "application/json; charset=" + (isUtf8 ? "UTF-8" : "gbk")); } wb.Headers.Add("ContentLength", postData.Length.ToString()); b = wb.UploadData(url, "POST", postData); } html = encode.GetString(b); string c = BaseRegex.GetMatchRegexString(wb.ResponseHeaders["Content-Type"], "charset\\s*=\\s*(\\S+)"); if (!string.IsNullOrEmpty(c)) { c = c.Trim().ToLower().Replace("utf8", "utf-8"); } if (string.IsNullOrEmpty(c)) c = BaseRegex.GetMatchRegexString(html, @"]*?charset=[""']*([\w-]+)[""']*"); if (!string.IsNullOrEmpty(c)) { if (c.IndexOf("'") > -1) c = c.Substring(0, c.IndexOf("'")); if (c.IndexOf("\"") > -1) c = c.Substring(0, c.IndexOf("\"")); if (c.IndexOf(" ") > -1) c = c.Substring(0, c.IndexOf(" ")); if (c.IndexOf(">") > -1) c = c.Substring(0, c.IndexOf(">")); if (!string.IsNullOrEmpty(c)) html = Encoding.GetEncoding(c).GetString(b); } head = wb.ResponseHeaders.ToString(); } catch (Exception ee) { html = "提交错误:" + ee.Message; head = ""; //if (!againt) //{ // againt = true; // Thread.Sleep(10 * 1000); // goto Againt; //} //if (!againt&&(ee.Message.Contains("目标机器")||(ee.Message.Contains("服务器")))) //{ // againt = true; // GetIPS(); // Thread.Sleep(10 * 1000); // goto Againt; //} } return GetHtml.Decode(html); } catch (Exception e) { LogSave.Add("访问网址错误:" + url + " " + e.Message); return ""; } } public static string UploadTaobaoNewPost(string url, string refere, bool isUtf8, string data, bool isPost, string cookies, WebProxy proxyObject) { bool againt = false; try { cookies = ""; string pdd = ""; if (!string.IsNullOrEmpty(cookies)) pdd = BaseRegex.GetMatchRegexString(ref cookies, "PDDAccessToken=([^;]+?);"); string pddx = ""; if (!string.IsNullOrEmpty(cookies)) pddx = BaseRegex.GetMatchRegexString(ref cookies, "PASS_ID=([^;]+?);"); Againt: string userAgent = UserAgentList[new Random(Guid.NewGuid().GetHashCode()).Next(0, UserAgentList.Count)]; if (!string.IsNullOrEmpty(userAgent) && !userAgent.Contains("MicroMessenger")) { userAgent = userAgent.Insert(new Random().Next(userAgent.Length), "MicroMessenger" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, new Random().Next(5))); } WebClientx wb = new WebClientx(); wb.Timeout = 10; wb.AllowAutoRedirect = true; wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); wb.Headers.Add("Accept-Encoding", "gzip,deflate"); wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); wb.Headers.Add("User-Agent", userAgent); //SetAuthorization(wb); wb.Proxy = null; if (!string.IsNullOrEmpty(refere)) wb.Headers.Add("Referer", refere); if (!string.IsNullOrEmpty(cookies)) wb.Headers.Add("Cookie", cookies); wb.Proxy = proxyObject; string rehead = wb.Headers.ToString(); var encode = isUtf8 ? Encoding.UTF8 : Encoding.GetEncoding("gbk"); byte[] b, postData = encode.GetBytes(data); string html, head; try { if (string.IsNullOrEmpty(data) && !isPost) { b = wb.DownloadData(url); } else { if (isPost || url.Contains("taobao.com")) { wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); } else { wb.Headers.Add("Content-Type", "application/json; charset=" + (isUtf8 ? "UTF-8" : "gbk")); } wb.Headers.Add("ContentLength", postData.Length.ToString()); b = wb.UploadData(url, "POST", postData); } html = encode.GetString(b); string c = BaseRegex.GetMatchRegexString(wb.ResponseHeaders["Content-Type"], "charset\\s*=\\s*(\\S+)"); if (!string.IsNullOrEmpty(c)) { c = c.Trim().ToLower().Replace("utf8", "utf-8"); } if (string.IsNullOrEmpty(c)) c = BaseRegex.GetMatchRegexString(html, @"]*?charset=[""']*([\w-]+)[""']*"); if (!string.IsNullOrEmpty(c)) { if (c.IndexOf("'") > -1) c = c.Substring(0, c.IndexOf("'")); if (c.IndexOf("\"") > -1) c = c.Substring(0, c.IndexOf("\"")); if (c.IndexOf(" ") > -1) c = c.Substring(0, c.IndexOf(" ")); if (c.IndexOf(">") > -1) c = c.Substring(0, c.IndexOf(">")); if (!string.IsNullOrEmpty(c)) html = Encoding.GetEncoding(c).GetString(b); } head = wb.ResponseHeaders.ToString(); } catch (Exception ee) { html = "提交错误:" + ee.Message; head = ""; //if (!againt) //{ // againt = true; // Thread.Sleep(10 * 1000); // goto Againt; //} //if (!againt&&(ee.Message.Contains("目标机器")||(ee.Message.Contains("服务器")))) //{ // againt = true; // GetIPS(); // Thread.Sleep(10 * 1000); // goto Againt; //} } return GetHtml.Decode(html); } catch (Exception e) { LogSave.Add("访问网址错误:" + url + " " + e.Message); return ""; } } public static string UploadTaobaoNewCePost(string url, string refere, bool isUtf8, string data, bool isPost, string cookies, WebProxy proxyObject) { bool againt = false; try { string pdd = ""; if (!string.IsNullOrEmpty(cookies)) pdd = BaseRegex.GetMatchRegexString(ref cookies, "PDDAccessToken=([^;]+?);"); string pddx = ""; if (!string.IsNullOrEmpty(cookies)) pddx = BaseRegex.GetMatchRegexString(ref cookies, "PASS_ID=([^;]+?);"); Againt: string userAgent = UserAgentList[new Random(Guid.NewGuid().GetHashCode()).Next(0, UserAgentList.Count)]; if (!string.IsNullOrEmpty(userAgent) && !userAgent.Contains("MicroMessenger")) { userAgent = userAgent.Insert(new Random().Next(userAgent.Length), "MicroMessenger" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, new Random().Next(5))); } WebClientx wb = new WebClientx(); wb.Timeout = 10; wb.AllowAutoRedirect = true; wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); wb.Headers.Add("Accept-Encoding", "gzip,deflate"); wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); wb.Headers.Add("User-Agent", userAgent); if (!string.IsNullOrEmpty(cookies) && cookies.Contains("PDDAccessToken") && BaseClass.CookiesAll.ContainsKey("yangkeduo.com") && BaseClass.CookiesAll["yangkeduo.com"].ContainsKey("PDDAccessToken")) wb.Headers.Add("accesstoken", BaseClass.CookiesAll["yangkeduo.com"]["PDDAccessToken"]); else if (!string.IsNullOrEmpty(pdd)) wb.Headers.Add("accesstoken", pdd); //SetAuthorization(wb); wb.Proxy = null; if (!string.IsNullOrEmpty(refere)) wb.Headers.Add("Referer", refere); if (!string.IsNullOrEmpty(cookies)) wb.Headers.Add("Cookie", cookies); wb.Proxy = proxyObject; string rehead = wb.Headers.ToString(); var encode = isUtf8 ? Encoding.UTF8 : Encoding.GetEncoding("gbk"); byte[] b, postData = encode.GetBytes(data); string html, head; try { if (string.IsNullOrEmpty(data) && !isPost) { b = wb.DownloadData(url); } else { if (isPost || url.Contains("taobao.com")) { wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); } else { wb.Headers.Add("Content-Type", "application/json; charset=" + (isUtf8 ? "UTF-8" : "gbk")); } wb.Headers.Add("ContentLength", postData.Length.ToString()); b = wb.UploadData(url, "POST", postData); } html = encode.GetString(b); string c = BaseRegex.GetMatchRegexString(wb.ResponseHeaders["Content-Type"], "charset\\s*=\\s*(\\S+)"); if (!string.IsNullOrEmpty(c)) { c = c.Trim().ToLower().Replace("utf8", "utf-8"); } if (string.IsNullOrEmpty(c)) c = BaseRegex.GetMatchRegexString(html, @"]*?charset=[""']*([\w-]+)[""']*"); if (!string.IsNullOrEmpty(c)) { if (c.IndexOf("'") > -1) c = c.Substring(0, c.IndexOf("'")); if (c.IndexOf("\"") > -1) c = c.Substring(0, c.IndexOf("\"")); if (c.IndexOf(" ") > -1) c = c.Substring(0, c.IndexOf(" ")); if (c.IndexOf(">") > -1) c = c.Substring(0, c.IndexOf(">")); if (!string.IsNullOrEmpty(c)) html = Encoding.GetEncoding(c).GetString(b); } head = wb.ResponseHeaders.ToString(); } catch (Exception ee) { html = "提交错误:" + ee.Message; head = ""; //if (!againt) //{ // againt = true; // Thread.Sleep(10 * 1000); // goto Againt; //} //if (!againt&&(ee.Message.Contains("目标机器")||(ee.Message.Contains("服务器")))) //{ // againt = true; // GetIPS(); // Thread.Sleep(10 * 1000); // goto Againt; //} } return GetHtml.Decode(html); } catch (Exception e) { LogSave.Add("访问网址错误:" + url + " " + e.Message); return ""; } } public static string UploadTaobaoPost(string url, string refere, bool isUtf8, string data, bool isPost, string cookies) { bool againt = false; try { //cookies = GetCookies(url, cookies); //if (string.IsNullOrEmpty(cookies)) // cookies = BaseClass.ChromeCookies; cookies = ""; string pdd = ""; if (!string.IsNullOrEmpty(cookies)) pdd = BaseRegex.GetMatchRegexString(ref cookies, "PDDAccessToken=([^;]+?);"); string pddx = ""; if (!string.IsNullOrEmpty(cookies)) pddx = BaseRegex.GetMatchRegexString(ref cookies, "PASS_ID=([^;]+?);"); Againt: string userAgent = UserAgentList[new Random(Guid.NewGuid().GetHashCode()).Next(0, UserAgentList.Count)]; WebClientx wb = new WebClientx(); wb.Timeout = 30; wb.AllowAutoRedirect = true; wb.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01"); wb.Headers.Add("X-Requested-With", "XMLHttpRequest"); wb.Headers.Add("Accept-Encoding", "gzip,deflate"); SetAuthorization(wb); if (!string.IsNullOrEmpty(cookies) && cookies.Contains("PDDAccessToken") && BaseClass.CookiesAll.ContainsKey("yangkeduo.com") && BaseClass.CookiesAll["yangkeduo.com"].ContainsKey("PDDAccessToken")) wb.Headers.Add("accesstoken", BaseClass.CookiesAll["yangkeduo.com"]["PDDAccessToken"]); else if (!string.IsNullOrEmpty(pdd)) wb.Headers.Add("accesstoken", pdd); else if (!string.IsNullOrEmpty(pddx)) wb.Headers.Add("PASSID", pddx); if (url.Contains("gy.taobao.com")) wb.Headers.Add("Origin", "https://gy.taobao.com"); if (url.Contains("mms.pinduoduo.com")) wb.Headers.Add("Origin", "http://mms.pinduoduo.com"); wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); wb.Headers.Add("User-Agent", userAgent); wb.Proxy = null; if (!string.IsNullOrEmpty(refere)) wb.Headers.Add("Referer", refere); if (!string.IsNullOrEmpty(cookies)) wb.Headers.Add("Cookie", cookies); wb.Proxy = null; string rehead = wb.Headers.ToString(); var encode = isUtf8 ? Encoding.UTF8 : Encoding.GetEncoding("gbk"); byte[] b, postData = encode.GetBytes(data); string html, head; try { if (string.IsNullOrEmpty(data) && !isPost) { b = wb.DownloadData(url); } else { if (isPost || url.Contains("taobao.com")) { wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); } else { wb.Headers.Add("Content-Type", "application/json; charset=" + (isUtf8 ? "UTF-8" : "gbk")); } wb.Headers.Add("ContentLength", postData.Length.ToString()); b = wb.UploadData(url, "POST", postData); } html = encode.GetString(b); string c = BaseRegex.GetMatchRegexString(wb.ResponseHeaders["Content-Type"], "charset\\s*=\\s*(\\S+)"); if (!string.IsNullOrEmpty(c)) { c = c.Trim().ToLower().Replace("utf8", "utf-8"); } if (string.IsNullOrEmpty(c)) c = BaseRegex.GetMatchRegexString(html, @"]*?charset=[""']*([\w-]+)[""']*"); if (!string.IsNullOrEmpty(c)) { if (c.IndexOf("'") > -1) c = c.Substring(0, c.IndexOf("'")); if (c.IndexOf("\"") > -1) c = c.Substring(0, c.IndexOf("\"")); if (c.IndexOf(" ") > -1) c = c.Substring(0, c.IndexOf(" ")); if (c.IndexOf(">") > -1) c = c.Substring(0, c.IndexOf(">")); if (!string.IsNullOrEmpty(c)) html = Encoding.GetEncoding(c).GetString(b); } head = wb.ResponseHeaders.ToString(); } catch (Exception ee) { html = "提交错误:" + ee.Message; head = ""; if (!againt) { againt = true; Thread.Sleep(10 * 1000); goto Againt; } } return GetHtml.Decode(html); } catch (Exception e) { LogSave.Add("访问网址错误:" + url + " " + e.Message); return ""; } } public static int GetJsonPostStatus(string url) { try { string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.1.62 Safari/537.36 PddWorkbench-Online PddBrowser"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);// request.KeepAlive = true; request.Accept = "application/json"; request.UserAgent = userAgent; request.ContentType = "application/json;charset=UTF-8"; request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br"); request.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9"); request.Method = "POST"; request.ServicePoint.Expect100Continue = false; string body = @"{}"; byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body); request.ContentLength = postBytes.Length; Stream stream = request.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { return response.StatusCode.ParseToInt(); } } catch { } return 0; } public static string GetCookies(string url, string cookies) { if (!string.IsNullOrEmpty(cookies)) return cookies; string mainDomain = GetHtml.GetMainDomain(GetHtml.GetDomain(url)); if (BaseClass.CookiesAllString.ContainsKey(mainDomain)) cookies = BaseClass.CookiesAllString[mainDomain]; return cookies; } public static void SetAuthorization(WebClientx wb) { long timeStamp = GetUnixSecondTimeStamp(DateTime.Now) + BaseClass.TimeDifferent; string hexadecimalTimeStamp = timeStamp.ToString("X"); string token = HMACSHA256(hexadecimalTimeStamp, ProxyIpKey).ToUpper(); wb.Headers.Remove(headerKey); wb.Headers.Add(headerKey, string.Format("{0}|{1}", hexadecimalTimeStamp, token)); } public static int GetUnixSecondTimeStamp(DateTime dt) { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); return (int)(dt - startTime).TotalSeconds; } public static string HMACSHA256(string encrypt, string key = "") { var encoding = new ASCIIEncoding(); byte[] keyByte = encoding.GetBytes(key); byte[] encryptByte = encoding.GetBytes(encrypt); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(encryptByte); StringBuilder builder = new StringBuilder(); for (int i = 0; i < hashmessage.Length; i++) { builder.Append(hashmessage[i].ToString("X2")); } return builder.ToString(); } } public static string UploadNewProxyJinBao(string url, string refere, bool isUtf8, string cookies, WebProxy proxyObject) { return UploadTaobaoNewJinBaoPost(url, refere, isUtf8, "", false, cookies, proxyObject); } public static string UploadNewProxyJinBaoPost(string url, string refere, bool isUtf8, string data, string cookies, WebProxy proxyObject) { return UploadTaobaoJinBaoPost(url, refere, isUtf8, data, false, cookies, proxyObject); } public static string UploadTaobaoNewJinBaoPost(string url, string refere, bool isUtf8, string data, bool isPost, string cookies, WebProxy proxyObject) { bool againt = false; try { //cookies = ""; string pdd = ""; if (!string.IsNullOrEmpty(cookies)) pdd = BaseRegex.GetMatchRegexString(ref cookies, "PDDAccessToken=([^;]+?);"); string pddx = ""; if (!string.IsNullOrEmpty(cookies)) pddx = BaseRegex.GetMatchRegexString(ref cookies, "PASS_ID=([^;]+?);"); Againt: string userAgent = UserAgentList[new Random(Guid.NewGuid().GetHashCode()).Next(0, UserAgentList.Count)]; WebClientx wb = new WebClientx(); wb.Timeout = 10; wb.AllowAutoRedirect = true; wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); wb.Headers.Add("Accept-Encoding", "gzip,deflate"); wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); wb.Headers.Add("User-Agent", userAgent); //SetAuthorization(wb); wb.Proxy = null; if (!string.IsNullOrEmpty(refere)) wb.Headers.Add("Referer", refere); if (!string.IsNullOrEmpty(cookies)) wb.Headers.Add("Cookie", cookies); wb.Proxy = proxyObject; string rehead = wb.Headers.ToString(); var encode = isUtf8 ? Encoding.UTF8 : Encoding.GetEncoding("gbk"); byte[] b, postData = encode.GetBytes(data); string html, head; try { if (string.IsNullOrEmpty(data) && !isPost) { b = wb.DownloadData(url); } else { if (isPost || url.Contains("taobao.com")) { wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); } else { wb.Headers.Add("Content-Type", "application/json; charset=" + (isUtf8 ? "UTF-8" : "gbk")); } wb.Headers.Add("ContentLength", postData.Length.ToString()); b = wb.UploadData(url, "POST", postData); } html = encode.GetString(b); string c = BaseRegex.GetMatchRegexString(wb.ResponseHeaders["Content-Type"], "charset\\s*=\\s*(\\S+)"); if (!string.IsNullOrEmpty(c)) { c = c.Trim().ToLower().Replace("utf8", "utf-8"); } if (string.IsNullOrEmpty(c)) c = BaseRegex.GetMatchRegexString(html, @"]*?charset=[""']*([\w-]+)[""']*"); if (!string.IsNullOrEmpty(c)) { if (c.IndexOf("'") > -1) c = c.Substring(0, c.IndexOf("'")); if (c.IndexOf("\"") > -1) c = c.Substring(0, c.IndexOf("\"")); if (c.IndexOf(" ") > -1) c = c.Substring(0, c.IndexOf(" ")); if (c.IndexOf(">") > -1) c = c.Substring(0, c.IndexOf(">")); if (!string.IsNullOrEmpty(c)) html = Encoding.GetEncoding(c).GetString(b); } head = wb.ResponseHeaders.ToString(); } catch (Exception ee) { html = "提交错误:" + ee.Message; head = ""; //if (!againt) //{ // againt = true; // Thread.Sleep(10 * 1000); // goto Againt; //} //if (!againt&&(ee.Message.Contains("目标机器")||(ee.Message.Contains("服务器")))) //{ // againt = true; // GetIPS(); // Thread.Sleep(10 * 1000); // goto Againt; //} } return GetHtml.Decode(html); } catch (Exception e) { LogSave.Add("访问网址错误:" + url + " " + e.Message); return ""; } } public static string UploadTaobaoJinBaoPost(string url, string refere, bool isUtf8, string data, bool isPost, string cookies, WebProxy proxyObject) { bool againt = false; try { cookies = GetCookies(url, cookies); if (string.IsNullOrEmpty(cookies)) cookies = BaseClass.ChromeCookies; string pdd = ""; if (!string.IsNullOrEmpty(cookies)) pdd = BaseRegex.GetMatchRegexString(ref cookies, "PDDAccessToken=([^;]+?);"); string pddx = ""; if (!string.IsNullOrEmpty(cookies)) pddx = BaseRegex.GetMatchRegexString(ref cookies, "PASS_ID=([^;]+?);"); Againt: string userAgent = BaseClass.ChromeUserAgent; WebClientx wb = new WebClientx(); wb.Timeout = 3; wb.AllowAutoRedirect = true; wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); wb.Headers.Add("Accept-Encoding", "gzip,deflate"); wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); wb.Headers.Add("User-Agent", userAgent); wb.Proxy = null; if (!string.IsNullOrEmpty(refere)) wb.Headers.Add("Referer", refere); if (!string.IsNullOrEmpty(cookies)) wb.Headers.Add("Cookie", cookies); wb.Proxy = proxyObject; string rehead = wb.Headers.ToString(); var encode = isUtf8 ? Encoding.UTF8 : Encoding.GetEncoding("gbk"); byte[] b, postData = encode.GetBytes(data); string html, head; try { if (string.IsNullOrEmpty(data) && !isPost) { b = wb.DownloadData(url); } else { if (isPost || url.Contains("taobao.com")) { wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); } else { wb.Headers.Add("Content-Type", "application/json; charset=" + (isUtf8 ? "UTF-8" : "gbk")); } wb.Headers.Add("ContentLength", postData.Length.ToString()); b = wb.UploadData(url, "POST", postData); } html = encode.GetString(b); string c = BaseRegex.GetMatchRegexString(wb.ResponseHeaders["Content-Type"], "charset\\s*=\\s*(\\S+)"); if (!string.IsNullOrEmpty(c)) { c = c.Trim().ToLower().Replace("utf8", "utf-8"); } if (string.IsNullOrEmpty(c)) c = BaseRegex.GetMatchRegexString(html, @"]*?charset=[""']*([\w-]+)[""']*"); if (!string.IsNullOrEmpty(c)) { if (c.IndexOf("'") > -1) c = c.Substring(0, c.IndexOf("'")); if (c.IndexOf("\"") > -1) c = c.Substring(0, c.IndexOf("\"")); if (c.IndexOf(" ") > -1) c = c.Substring(0, c.IndexOf(" ")); if (c.IndexOf(">") > -1) c = c.Substring(0, c.IndexOf(">")); if (!string.IsNullOrEmpty(c)) html = Encoding.GetEncoding(c).GetString(b); } head = wb.ResponseHeaders.ToString(); } catch (Exception ee) { html = "提交错误:" + ee.Message; head = ""; //if (!againt) //{ // againt = true; // Thread.Sleep(10 * 1000); // goto Againt; //} //if (!againt&&(ee.Message.Contains("目标机器")||(ee.Message.Contains("服务器")))) //{ // againt = true; // GetIPS(); // Thread.Sleep(10 * 1000); // goto Againt; //} } return GetHtml.Decode(html); } catch (Exception e) { LogSave.Add("访问网址错误:" + url + " " + e.Message); return ""; } } } public enum ProxyTypeEnum { [Description("未知")] Unknown = 0, [Description("火箭代理 http://125.88.158.218/")] RocketDaiLi = 1, [Description("66代理")] SixSixDaiLi = 2, [Description("快代理")] KuaiDaiLi = 3 } public enum UserAgentTypeEnum { [Description("未知")] Unknown = 0, [Description("PC")] PC = 1, [Description("Mobile")] Mobile = 2, [Description("Pad")] Pad = 3 } public class HttpParam { public HttpParam() { ResponseEncoding = Encoding.UTF8; } public string Url { get; set; } public string Method { get; set; } public string PostData { get; set; } public string ContentType { get; set; } public string Cookie { get; set; } public string Referer { get; set; } public string UserAgent { get; set; } public UserAgentTypeEnum UserAgentType { get; set; } public string AcceptLanguage { get; set; } public string Accept { get; set; } [JsonIgnore] public WebProxy Proxy { get; set; } public int Timeout { get; set; } public Dictionary Header { get; set; } public int ProxySleep { get; set; } public int TryTimes { get; set; } public int ActualTryTimes { get; set; } [JsonIgnore] public CancellationTokenSource CancellationTokenSource { get; set; } [JsonIgnore] public Encoding ResponseEncoding { get; set; } } }