Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry on DoGetAsync failed #329

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 62 additions & 39 deletions src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,72 @@ public class HTTPUtil
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher,
};

private static async Task<HttpResponseMessage> DoGetAsync(string url, Dictionary<string, string>? headers = null)
private static async Task<HttpResponseMessage> DoGetAsync(string url,
Dictionary<string, string>? headers = null)
{
Logger.Debug(ResString.fetch + url);
using var webRequest = new HttpRequestMessage(HttpMethod.Get, url);
webRequest.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
webRequest.Headers.CacheControl = CacheControlHeaderValue.Parse("no-cache");
webRequest.Headers.Connection.Clear();
if (headers != null)
int retry = 3;
ReGet:
try
{
foreach (var item in headers)
Logger.Debug(ResString.fetch + url);
using var webRequest = new HttpRequestMessage(HttpMethod.Get, url);
webRequest.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
webRequest.Headers.CacheControl = CacheControlHeaderValue.Parse("no-cache");
webRequest.Headers.Connection.Clear();
if (headers != null)
{
webRequest.Headers.TryAddWithoutValidation(item.Key, item.Value);
}
}
Logger.Debug(webRequest.Headers.ToString());
//手动处理跳转,以免自定义Headers丢失
var webResponse = await AppHttpClient.SendAsync(webRequest, HttpCompletionOption.ResponseHeadersRead);
if (((int)webResponse.StatusCode).ToString().StartsWith("30"))
{
HttpResponseHeaders respHeaders = webResponse.Headers;
Logger.Debug(respHeaders.ToString());
if (respHeaders != null && respHeaders.Location != null)
{
var redirectedUrl = "";
if (!respHeaders.Location.IsAbsoluteUri)
{
Uri uri1 = new Uri(url);
Uri uri2 = new Uri(uri1, respHeaders.Location);
redirectedUrl = uri2.ToString();
}
else
foreach (var item in headers)
{
redirectedUrl = respHeaders.Location.AbsoluteUri;
webRequest.Headers.TryAddWithoutValidation(item.Key, item.Value);
}

if (redirectedUrl != url)
}

Logger.Debug(webRequest.Headers.ToString());
//手动处理跳转,以免自定义Headers丢失

var webResponse = await AppHttpClient.SendAsync(webRequest, HttpCompletionOption.ResponseHeadersRead);
if (((int)webResponse.StatusCode).ToString().StartsWith("30"))
{
HttpResponseHeaders respHeaders = webResponse.Headers;
Logger.Debug(respHeaders.ToString());
if (respHeaders != null && respHeaders.Location != null)
{
Logger.Extra($"Redirected => {redirectedUrl}");
return await DoGetAsync(redirectedUrl, headers);
var redirectedUrl = "";
if (!respHeaders.Location.IsAbsoluteUri)
{
Uri uri1 = new Uri(url);
Uri uri2 = new Uri(uri1, respHeaders.Location);
redirectedUrl = uri2.ToString();
}
else
{
redirectedUrl = respHeaders.Location.AbsoluteUri;
}

if (redirectedUrl != url)
{
Logger.Extra($"Redirected => {redirectedUrl}");
return await DoGetAsync(redirectedUrl, headers);
}
}
}

//手动将跳转后的URL设置进去, 用于后续取用
webResponse.Headers.Location = new Uri(url);
webResponse.EnsureSuccessStatusCode();
return webResponse;
}

catch (HttpRequestException e)
{
if (retry-- > 0)
{
Logger.Debug("Retry: {}, message: {}" ,retry,e.Message);
goto ReGet;
}

throw;
}
//手动将跳转后的URL设置进去, 用于后续取用
webResponse.Headers.Location = new Uri(url);
webResponse.EnsureSuccessStatusCode();
return webResponse;
}

public static async Task<byte[]> GetBytesAsync(string url, Dictionary<string, string>? headers = null)
Expand All @@ -76,6 +96,7 @@ public static async Task<byte[]> GetBytesAsync(string url, Dictionary<string, st
{
return await File.ReadAllBytesAsync(new Uri(url).LocalPath);
}

byte[] bytes = new byte[0];
var webResponse = await DoGetAsync(url, headers);
bytes = await webResponse.Content.ReadAsByteArrayAsync();
Expand Down Expand Up @@ -110,7 +131,8 @@ private static bool CheckMPEG2TS(HttpResponseMessage? webResponse)
/// <param name="url"></param>
/// <param name="headers"></param>
/// <returns>(Source Code, RedirectedUrl)</returns>
public static async Task<(string, string)> GetWebSourceAndNewUrlAsync(string url, Dictionary<string, string>? headers = null)
public static async Task<(string, string)> GetWebSourceAndNewUrlAsync(string url,
Dictionary<string, string>? headers = null)
{
string htmlCode = string.Empty;
var webResponse = await DoGetAsync(url, headers);
Expand All @@ -122,6 +144,7 @@ private static bool CheckMPEG2TS(HttpResponseMessage? webResponse)
{
htmlCode = await webResponse.Content.ReadAsStringAsync();
}

Logger.Debug(htmlCode);
return (htmlCode, webResponse.Headers.Location != null ? webResponse.Headers.Location.AbsoluteUri : url);
}
Expand All @@ -138,4 +161,4 @@ public static async Task<string> GetPostResponseAsync(string Url, byte[] postDat
return htmlCode;
}
}
}
}