From 6a72a807ad4ea0c9ef1238918dec21213146769d Mon Sep 17 00:00:00 2001 From: nilaoda Date: Sat, 12 Nov 2022 11:42:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96HTTP=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs | 2 +- src/N_m3u8DL-RE/Util/DownloadUtil.cs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs b/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs index 027f2d7..5b473fd 100644 --- a/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs +++ b/src/N_m3u8DL-RE.Common/Util/HTTPUtil.cs @@ -52,7 +52,7 @@ namespace N_m3u8DL_RE.Common.Util Logger.Debug(webRequest.Headers.ToString()); //手动处理跳转,以免自定义Headers丢失 var webResponse = await AppHttpClient.SendAsync(webRequest, HttpCompletionOption.ResponseHeadersRead); - if (webResponse.StatusCode == HttpStatusCode.Found || webResponse.StatusCode == HttpStatusCode.Moved || webResponse.StatusCode == HttpStatusCode.SeeOther) + if (((int)webResponse.StatusCode).ToString().StartsWith("30")) { HttpResponseHeaders respHeaders = webResponse.Headers; Logger.Debug(respHeaders.ToString()); diff --git a/src/N_m3u8DL-RE/Util/DownloadUtil.cs b/src/N_m3u8DL-RE/Util/DownloadUtil.cs index aa8abdc..46749e9 100644 --- a/src/N_m3u8DL-RE/Util/DownloadUtil.cs +++ b/src/N_m3u8DL-RE/Util/DownloadUtil.cs @@ -58,7 +58,7 @@ namespace N_m3u8DL_RE.Util try { using var response = await AppHttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token); - if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.SeeOther) + if (((int)response.StatusCode).ToString().StartsWith("30")) { HttpResponseHeaders respHeaders = response.Headers; Logger.Debug(respHeaders.ToString()); @@ -86,6 +86,13 @@ namespace N_m3u8DL_RE.Util using var responseStream = await response.Content.ReadAsStreamAsync(cancellationTokenSource.Token); var buffer = new byte[16 * 1024]; var size = 0; + + //检测imageHeader + size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token); + speedContainer.Add(size); + await stream.WriteAsync(buffer, 0, size); + bool imageHeader = ImageHeaderUtil.IsImageHeader(buffer); + while ((size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token)) > 0) { speedContainer.Add(size); @@ -96,7 +103,8 @@ namespace N_m3u8DL_RE.Util { ActualContentLength = stream.Length, RespContentLength = contentLength, - ActualFilePath = path + ActualFilePath = path, + ImageHeader= imageHeader }; } catch (OperationCanceledException oce) when (oce.CancellationToken == cancellationTokenSource.Token)