mirror of
https://github.com/lampame/lampac-ukraine.git
synced 2026-04-16 17:32:20 +00:00
refactor(uakino): replace Http.Get with custom GetString method
Implement a new GetString method using HttpClient for better control over HTTP requests including proxy handling, SSL validation, and timeout management. This replaces the previous Http.Get calls throughout the UAKino module to standardize request handling and improve reliability.
This commit is contained in:
parent
d3829323eb
commit
838dc96aa9
@ -1,8 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Security.Authentication;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using HtmlAgilityPack;
|
using HtmlAgilityPack;
|
||||||
@ -62,7 +66,7 @@ namespace UAKino
|
|||||||
};
|
};
|
||||||
|
|
||||||
_onLog?.Invoke($"UAKino search: {searchUrl}");
|
_onLog?.Invoke($"UAKino search: {searchUrl}");
|
||||||
string html = await Http.Get(searchUrl, headers: headers, proxy: _proxyManager.Get());
|
string html = await GetString(searchUrl, headers);
|
||||||
if (string.IsNullOrEmpty(html))
|
if (string.IsNullOrEmpty(html))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -147,7 +151,7 @@ namespace UAKino
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_onLog?.Invoke($"UAKino playlist: {url}");
|
_onLog?.Invoke($"UAKino playlist: {url}");
|
||||||
string payload = await Http.Get(url, headers: headers, proxy: _proxyManager.Get());
|
string payload = await GetString(url, headers);
|
||||||
if (string.IsNullOrEmpty(payload))
|
if (string.IsNullOrEmpty(payload))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@ -189,7 +193,7 @@ namespace UAKino
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_onLog?.Invoke($"UAKino movie page: {href}");
|
_onLog?.Invoke($"UAKino movie page: {href}");
|
||||||
string html = await Http.Get(href, headers: headers, proxy: _proxyManager.Get());
|
string html = await GetString(href, headers);
|
||||||
if (string.IsNullOrEmpty(html))
|
if (string.IsNullOrEmpty(html))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@ -232,7 +236,7 @@ namespace UAKino
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_onLog?.Invoke($"UAKino parse player: {url}");
|
_onLog?.Invoke($"UAKino parse player: {url}");
|
||||||
string html = await Http.Get(url, headers: headers, proxy: _proxyManager.Get());
|
string html = await GetString(url, headers);
|
||||||
if (string.IsNullOrEmpty(html))
|
if (string.IsNullOrEmpty(html))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@ -253,6 +257,45 @@ namespace UAKino
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<string> GetString(string url, List<HeadersModel> headers, int timeoutSeconds = 15)
|
||||||
|
{
|
||||||
|
var handler = new HttpClientHandler
|
||||||
|
{
|
||||||
|
AllowAutoRedirect = true,
|
||||||
|
AutomaticDecompression = DecompressionMethods.Brotli | DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
||||||
|
SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13
|
||||||
|
};
|
||||||
|
|
||||||
|
handler.ServerCertificateCustomValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
|
||||||
|
|
||||||
|
var proxy = _proxyManager.Get();
|
||||||
|
if (proxy != null)
|
||||||
|
{
|
||||||
|
handler.UseProxy = true;
|
||||||
|
handler.Proxy = proxy;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
handler.UseProxy = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var client = new HttpClient(handler);
|
||||||
|
using var req = new HttpRequestMessage(HttpMethod.Get, url);
|
||||||
|
|
||||||
|
if (headers != null)
|
||||||
|
{
|
||||||
|
foreach (var h in headers)
|
||||||
|
req.Headers.TryAddWithoutValidation(h.name, h.val);
|
||||||
|
}
|
||||||
|
|
||||||
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(Math.Max(5, timeoutSeconds)));
|
||||||
|
using var response = await client.SendAsync(req, cts.Token).ConfigureAwait(false);
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return await response.Content.ReadAsStringAsync(cts.Token).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
private List<PlaylistItem> ParsePlaylistHtml(string html)
|
private List<PlaylistItem> ParsePlaylistHtml(string html)
|
||||||
{
|
{
|
||||||
var items = new List<PlaylistItem>();
|
var items = new List<PlaylistItem>();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user