using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using DouYinPush.Model;
using DouYinPush.Model.DouYinVideo.New;
using Fandi.Common.Convert;
using Fandi.Common.Helper;
using Newtonsoft.Json;
namespace DouYinPush.Helper;
public class RequestOperate
{
private static T HttpGet<T>(string url, string cookie)
{
T val = default(T);
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
using WebClient webClient = new WebClient();
webClient.Headers.Add("Referer", "https://www.douyin.com/");
webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36");
webClient.Headers.Add("Cookie", cookie);
byte[] bytes = webClient.DownloadData(url);
string value = Encoding.UTF8.GetString(bytes);
return JsonConvert.DeserializeObject<T>(value);
}
catch (Exception)
{
throw;
}
}
private static T HttpPost<T>(string url, string param, string cookie)
{
T result = default(T);
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
using WebClient webClient = new WebClient();
byte[] bytes = Encoding.UTF8.GetBytes(param);
webClient.Headers.Add("Cookie", cookie);
byte[] bytes2 = webClient.UploadData(url, "POST", bytes);
string value = Encoding.UTF8.GetString(bytes2);
result = JsonConvert.DeserializeObject<T>(value);
return result;
}
catch (Exception ex)
{
if (!url.StartsWith("https://webcast.amemv.com/webcast/room/ping/anchor/"))
{
LogHelper.WriteException(ex, "HttpPost");
}
}
return result;
}
public static GetQrCodeResponse GetQrCodeInfo()
{
return HttpGet<GetQrCodeResponse>("https://sso.douyin.com/get_qrcode/?service=https%3A%2F%2Fwebcast.amemv.com&need_logo=false&aid=10006&account_sdk_source=sso&language=zh", "");
}
public static string GetHtml(string url)
{
string result = "";
try
{
WebRequest webRequest = WebRequest.Create(url);
webRequest.Method = "GET";
webRequest.Headers.Add("Cookie", CacheHelper.look_video_cookie);
WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
Encoding encoding = Encoding.Default;
StreamReader streamReader = new StreamReader(responseStream, encoding);
result = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
response.Close();
}
catch (Exception)
{
}
return result;
}
public static QueryScanQrCodeStatusResponse GetQrCodeStatus()
{
return HttpGet<QueryScanQrCodeStatusResponse>("https://sso.douyin.com/check_qrconnect/?service=https%3A%2F%2Fwebcast.amemv.com&token=" + CacheHelper.token + "&need_logo=false&is_frontier=false&aid=10006&account_sdk_source=sso&language=zh", "");
}
public static StreamUrlResponse GetPushUrl()
{
return HttpPost<StreamUrlResponse>("https://webcast.amemv.com/webcast/room/get_latest_room/?ac=wifi&app_name=webcast_mate&version_code=2.2.9&device_platform=windows&webcast_sdk_version=1520&resolution=1920%2A1080&os_version=10.0.19043&language=zh&aid=2079&live_id=1&channel=online&device_id=3096676051989080&iid=1117559680415880", "", CacheHelper.cookie);
}
public static void PingAnchor()
{
if (!CacheHelper.room_id.IsEmpty() && !CacheHelper.stream_id.IsEmpty())
{
HttpPost<RoomPingResponse>("https://webcast.amemv.com/webcast/room/ping/anchor/?ac=wifi&app_name=webcast_mate&version_code=2.2.9&device_platform=windows&webcast_sdk_version=1520&resolution=1920%2A1080&os_version=10.0.19043&language=zh&aid=2079&live_id=1&channel=online&device_id=3096676051989080&iid=1117559680415880", "stream_id=" + CacheHelper.stream_id + "&room_id=" + CacheHelper.room_id + "&status=2", CacheHelper.cookie);
}
}
public static LiveInfoItemEntity GetLiveInfoByID(string web_r_id)
{
return HttpGet<LiveInfoItemEntity>("https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=page_refresh&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=zh-CN&browser_platform=Win32&browser_name=Edge&browser_version=127.0.0.0&web_rid=" + web_r_id + "&enter_source=&is_need_double_stream=false&insert_task_id=&live_reason=", CacheHelper.cookie);
}
public static void CloseLive()
{
if (!CacheHelper.stream_id.IsEmpty() && !CacheHelper.room_id.IsEmpty() && !CacheHelper.cookie.IsEmpty())
{
RoomPingResponse roomPingResponse = HttpPost<RoomPingResponse>("https://webcast.amemv.com/webcast/room/ping/anchor/?ac=wifi&app_name=webcast_mate&version_code=8.6.1&device_platform=windows&webcast_sdk_version=1520&resolution=1920%2A1080&os_version=10.0.19045&language=zh&aid=2079&live_id=1&channel=online&device_id=4229146318764409&iid=2566679780145658&msToken=Q6DSpfUwpMfquGjBJTh2oplGdt2OqLSidNY-HegSpQysL8_lZgft_GgLedjiJvtseHO0AU8sU4cJT5woBO8hG4Kt8cR5NvW5l0GjqSsJJK4-gxLL0w==&X-Bogus=DFSzswVuZFVuRe0FtQCB6rl849N7&_signature=_02B4Z6wo00001uxlUIwAAIDBRSaf3TbcWzbsZVQAANwPrarTWR064S2XsK2HpvtNj1lRBCGX5VsHWhCGawcNNtUheDXzOgM4dCF3J3bOFDSX8cIdchJRQDmHOpFyiLhZ3ReEUoH4Vow.QTj54c", "stream_id=" + CacheHelper.stream_id + "&room_id=" + CacheHelper.room_id + "&status=4", CacheHelper.cookie);
if (roomPingResponse.status_code == 0)
{
MessageHelper.WarningMsg("直播已关闭!");
return;
}
MessageHelper.WarningMsg(roomPingResponse.data.prompts + "(" + roomPingResponse.data.message + "-" + roomPingResponse.status_code + ")");
}
else
{
MessageHelper.WarningMsg("未检测到开播信息!");
}
}
public static string RequestUrl(string url)
{
return GetRedirectUrl(url);
}
public static string GetRedirectUrl(string url, int type = 0, string cookies = "")
{
string result = "";
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "HEAD";
httpWebRequest.AllowAutoRedirect = false;
string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
if (type > 0)
{
userAgent = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
}
httpWebRequest.UserAgent = userAgent;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
if (!cookies.IsEmpty())
{
httpWebRequest.Headers.Add("Cookie", cookies);
}
using WebResponse webResponse = httpWebRequest.GetResponse();
result = webResponse.Headers["Location"];
}
catch (Exception)
{
throw new Exception("地址重定向错误!");
}
return result;
}
public static bool DownMP4(string url, string savePath)
{
bool result = false;
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Referer = url;
WebResponse response = httpWebRequest.GetResponse();
if (response.ContentType.ToLower().Length > 0)
{
using (Stream stream = response.GetResponseStream())
{
using FileStream fileStream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
byte[] array = new byte[1024];
int num = 0;
while ((num = stream.Read(array, 0, array.Length)) > 0)
{
fileStream.Write(array, 0, num);
}
}
result = true;
}
}
catch (Exception ex)
{
LogHelper.WriteException(ex);
}
return result;
}
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}