342 lines
14 KiB
C#
342 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AllBase.Helper
|
|
{
|
|
public class HttpHelper
|
|
{
|
|
|
|
public static string Request_mobile_yangkeduo_com(string url, string referer, string data, bool isUtf8, string cookie)
|
|
{
|
|
string responseText = "";
|
|
HttpWebResponse response = null;
|
|
try
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
request.KeepAlive = true;
|
|
request.Headers.Add("sec-ch-ua", @"""Chromium"";v=""104"", "" Not A;Brand"";v=""99"", ""Google Chrome"";v=""104""");
|
|
request.Headers.Add("AccessToken", BaseRegex.GetMatchRegexString(ref cookie, "PDDAccessToken=([^:]+?);"));
|
|
request.Headers.Add("sec-ch-ua-mobile", @"?0");
|
|
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36";
|
|
request.ContentType = "application/json;charset=UTF-8";
|
|
request.Accept = "application/json, text/plain, */*";
|
|
request.Headers.Add("sec-ch-ua-platform", @"""Windows""");
|
|
request.Headers.Add("Origin", @"https://mobile.yangkeduo.com");
|
|
request.Headers.Add("Sec-Fetch-Site", @"same-origin");
|
|
request.Headers.Add("Sec-Fetch-Mode", @"cors");
|
|
request.Headers.Add("Sec-Fetch-Dest", @"empty");
|
|
request.Referer = referer;
|
|
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
|
|
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9");
|
|
request.Headers.Set(HttpRequestHeader.Cookie, cookie);
|
|
|
|
request.Method = "POST";
|
|
request.ServicePoint.Expect100Continue = false;
|
|
|
|
if (data.Contains("0aoanti_content"))
|
|
{
|
|
string token = HttpHelper.GetAntiContent0ap(cookie, request.UserAgent, referer);
|
|
data = data.Replace("0aoanti_content", token);
|
|
}
|
|
string body = data;
|
|
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
|
|
request.ContentLength = postBytes.Length;
|
|
Stream stream = request.GetRequestStream();
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
stream.Close();
|
|
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
}
|
|
catch (WebException e)
|
|
{
|
|
if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
|
|
else responseText = "error," + e.Message;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
responseText = "error," + ex.Message;
|
|
}
|
|
if (response != null)
|
|
{
|
|
if (string.IsNullOrEmpty(responseText))
|
|
responseText = ReadResponse(response);
|
|
response.Close();
|
|
}
|
|
return responseText;
|
|
}
|
|
|
|
private static string ReadResponse(HttpWebResponse response)
|
|
{
|
|
using (Stream responseStream = response.GetResponseStream())
|
|
{
|
|
Stream streamToRead = responseStream;
|
|
if (response.ContentEncoding.ToLower().Contains("gzip"))
|
|
{
|
|
streamToRead = new System.IO.Compression.GZipStream(streamToRead, System.IO.Compression.CompressionMode.Decompress);
|
|
}
|
|
else if (response.ContentEncoding.ToLower().Contains("deflate"))
|
|
{
|
|
streamToRead = new System.IO.Compression.DeflateStream(streamToRead, System.IO.Compression.CompressionMode.Decompress);
|
|
}
|
|
|
|
if (response.CharacterSet.ToLower() == "gbk")
|
|
{
|
|
using (StreamReader streamReader = new StreamReader(streamToRead, Encoding.GetEncoding("gbk")))
|
|
{
|
|
return streamReader.ReadToEnd();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
using (StreamReader streamReader = new StreamReader(streamToRead, Encoding.UTF8))
|
|
{
|
|
return streamReader.ReadToEnd();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string Request_pifa_pinduoduo_com(string url, string referer, string datas, string cookies)
|
|
{
|
|
try
|
|
{
|
|
HttpWebResponse response = null;
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.KeepAlive = true;
|
|
request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0");
|
|
request.Headers.Add("sec-ch-ua", @"""Chromium"";v=""104"", "" Not A;Brand"";v=""99"", ""Google Chrome"";v=""104""");
|
|
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36";
|
|
if (!string.IsNullOrEmpty(cookies) && cookies.Contains("PDDAccessToken"))
|
|
{
|
|
request.Headers.Add("AccessToken", BaseRegex.GetMatchRegexString(ref cookies, "PDDAccessToken=([^:]+?);"));
|
|
}
|
|
else
|
|
{
|
|
string token = HttpHelper.GetAntiContent0ap(cookies, request.UserAgent, referer);
|
|
request.Headers.Add("Anti-Content", token);
|
|
}
|
|
|
|
request.ContentType = "application/json";
|
|
request.Headers.Add("sec-ch-ua-mobile", @"?0");
|
|
request.Headers.Add("sec-ch-ua-platform", @"""Windows""");
|
|
request.Accept = "*/*";
|
|
request.Headers.Add("Origin", referer);
|
|
request.Headers.Add("Sec-Fetch-Site", @"same-origin");
|
|
request.Headers.Add("Sec-Fetch-Mode", @"cors");
|
|
request.Headers.Add("Sec-Fetch-Dest", @"empty");
|
|
request.Referer = referer;
|
|
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "deflate");
|
|
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9");
|
|
request.Headers.Set(HttpRequestHeader.Cookie, cookies);
|
|
request.Method = "POST";
|
|
request.ServicePoint.Expect100Continue = false;
|
|
|
|
string body = datas;
|
|
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
|
|
request.ContentLength = postBytes.Length;
|
|
|
|
using (Stream requestStream = request.GetRequestStream())
|
|
{
|
|
requestStream.Write(postBytes, 0, postBytes.Length);
|
|
}
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
Stream receiveStream = response.GetResponseStream();
|
|
StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("UTF-8"));
|
|
string retdata = readStream.ReadToEnd().Trim();
|
|
return retdata;
|
|
}
|
|
catch (WebException e)
|
|
{
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public static string GetUrlCodex(string url)
|
|
{
|
|
WebClientx webClient = new WebClientx();
|
|
try
|
|
{
|
|
webClient.Timeout = 8;
|
|
webClient.Headers.Add("Accept-Encoding", "gzip,deflate");
|
|
webClient.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
|
|
webClient.Headers.Add("User-Agent", BaseClass.PddShopChromeUserAgent);
|
|
webClient.Proxy = null;
|
|
byte[] b = webClient.DownloadData(url);
|
|
b = Decompress(b);
|
|
string contentType = webClient.ResponseHeaders["Content-Type"];
|
|
//webClient.Dispose();
|
|
//webClient = null;
|
|
string html = "";
|
|
if (!String.IsNullOrEmpty(contentType))
|
|
{
|
|
string c = BaseRegex.GetMatchRegexString(contentType, "charset\\s*=\\s*(\\S+)");
|
|
if (!String.IsNullOrEmpty(c))
|
|
{
|
|
c = c.Trim();
|
|
html = Encoding.GetEncoding(c).GetString(b);
|
|
}
|
|
}
|
|
if (String.IsNullOrEmpty(html))
|
|
{
|
|
html = Encoding.GetEncoding("UTF-8").GetString(b);
|
|
}
|
|
return html;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
try
|
|
{
|
|
webClient.Dispose();
|
|
webClient = null;
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
public static List<string> url0aplist = new List<string>();
|
|
public static string GetAntiContent0ap(string cookie, string userAgent, string referer)
|
|
{
|
|
if (url0aplist.Count == 0)
|
|
{
|
|
string get0oaurl = "http://order.qixiaoke.com/?obj=" + BaseClass.routeInUse + "/config&act=anti_content_0ap";
|
|
string htmlsss = GetHtml.PostUtf8(get0oaurl, GetSignaturex("", ""));
|
|
Newtonsoft.Json.Linq.JArray jarr = Newtonsoft.Json.Linq.JArray.Parse(htmlsss);
|
|
foreach (var jobj in jarr)
|
|
{
|
|
url0aplist.Add(jobj["url"].ToString());
|
|
}
|
|
}
|
|
|
|
string url = url0aplist[new Random().Next(0, url0aplist.Count() - 1)] + "/getAntiContent?ua=" + userAgent + "&href=" + GetHtml.UrlEncodeUtf8(referer) + "&api_uid=&_nano_fp=";
|
|
int time = BaseClass.ConvertDateTimeInt(BaseClass.GetServerTimeString());
|
|
string time1 = BaseClass.GetMd5(time + "W*%IvTrqSVRaEOyN17@J");
|
|
string data = "&time=" + time + "&code=" + time1;
|
|
string html = GetUrlCodex(url + data);
|
|
if (string.IsNullOrEmpty(html))
|
|
{
|
|
url = url0aplist[new Random().Next(0, url0aplist.Count() - 1)] + "/getAntiContent?ua=" + userAgent + "&href=" + GetHtml.UrlEncodeUtf8(referer) + "&api_uid=&_nano_fp=";
|
|
html = GetUrlCodex(url + data);
|
|
}
|
|
return html;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ZIP解压
|
|
/// </summary>
|
|
/// <param name="zippedData"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Decompress(byte[] zippedData)
|
|
{
|
|
if (zippedData == null || zippedData.Length == 0)
|
|
return null;
|
|
if (zippedData.Length < 100)
|
|
return zippedData;
|
|
if (zippedData[2] != 0x8 || zippedData[3] != 0x0 || zippedData[4] != 0x0
|
|
|| zippedData[5] != 0x0 || zippedData[6] != 0x0 || zippedData[7] != 0x0
|
|
|| zippedData[0] != 31 || zippedData[1] != 139)//软加速判断头尾是否正确
|
|
return zippedData;
|
|
try
|
|
{
|
|
MemoryStream ms = new MemoryStream(zippedData);
|
|
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
|
|
MemoryStream outBuffer = new MemoryStream();
|
|
byte[] block = new byte[1024];
|
|
while (true)
|
|
{
|
|
int bytesRead = compressedzipStream.Read(block, 0, block.Length);
|
|
if (bytesRead < 1)
|
|
break;
|
|
outBuffer.Write(block, 0, bytesRead);
|
|
}
|
|
compressedzipStream.Close();
|
|
return outBuffer.ToArray();
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
return zippedData;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取签名字符串
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetSignature(bool isPhoneOrder = false)
|
|
{
|
|
int time = BaseClass.DateTimeToTimestampSeconds(DateTime.Now) + BaseClass.TimeDifferent;
|
|
string code = Encryption(time);
|
|
if (isPhoneOrder)
|
|
{
|
|
code = BaseClass.GetMd5(time.ToString() + "8eLg*MDD46YjA_e*onDgnvkdIaGhi=7B").ToUpper();
|
|
}
|
|
return "time=" + time + "&code=" + code + "&NbCodeVersion=1";
|
|
}
|
|
/// <summary>
|
|
/// 获取签名字符串,新版
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetSignaturex(bool isPhoneOrder = false, int posttype = 0)
|
|
{
|
|
return GetSignaturex(BaseClass.ShopId, BaseClass.ShopName, isPhoneOrder, posttype);
|
|
}
|
|
|
|
public static string GetSignaturex(string shopId, string shopName, bool isPhoneOrder = false, int posttype = 0)
|
|
{
|
|
if (string.IsNullOrEmpty(shopId))
|
|
shopId = BaseClass.ShopId;
|
|
if (string.IsNullOrEmpty(shopName))
|
|
shopName = BaseClass.ShopName;
|
|
int time = BaseClass.ConvertDateTimeInt(DateTime.Now) + BaseClass.TimeDifferent;
|
|
string time1 = Encryption(time);
|
|
if (posttype == 1)
|
|
{
|
|
return time1;
|
|
}
|
|
return GetSignature() + "&version=" + BaseClass.VersionCount + "&uid=" + BaseClass.UserId + "&key=" + BaseClass.UserKey +
|
|
"&cid=" + BaseClass.CompanyId + "&company=" + GetHtml.UrlEncodeUtf8(BaseClass.CompanyName) +
|
|
"&sid=" + shopId + "&shop=" + GetHtml.UrlEncodeUtf8(shopName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 签名算法
|
|
/// </summary>
|
|
/// <param name="s">随机数</param>
|
|
/// <returns>加密签名结果</returns>
|
|
public static string Encryption(int s)
|
|
{
|
|
return GetMd5(s + "sdhewu*/fhAAue48485ew5+5%@3485esGFFHFD");
|
|
}
|
|
/// <summary>
|
|
/// MD5加密
|
|
/// </summary>
|
|
/// <param name="instring"></param>
|
|
/// <returns></returns>
|
|
public static string GetMd5(string instring)
|
|
{
|
|
byte[] result = Encoding.Default.GetBytes(instring.Trim());
|
|
MD5 md5 = new MD5CryptoServiceProvider();
|
|
byte[] output = md5.ComputeHash(result);
|
|
return BitConverter.ToString(output).Replace("-", "");
|
|
}
|
|
}
|
|
}
|