|
7 | 7 |
|
8 | 8 | #if NETCOREAPP3_1_OR_GREATER |
9 | 9 | using System.Net.Http; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | +using System.Net.Http.Headers; |
10 | 12 | #endif |
11 | 13 | using System.Runtime.InteropServices; |
12 | 14 | using System.Security.AccessControl; |
@@ -168,14 +170,66 @@ protected override async Task<HttpResponseMessage> GetResponseAsync(HttpRequestM |
168 | 170 | var response = await base.GetResponseAsync(request); |
169 | 171 | if (string.IsNullOrEmpty(ServerSuggestionFileName)) |
170 | 172 | { |
| 173 | + ServerSuggestionFileName = TryGetServerSuggestionFileName(); |
| 174 | + } |
| 175 | + |
| 176 | + return response; |
| 177 | + |
| 178 | + string? TryGetServerSuggestionFileName() |
| 179 | + { |
| 180 | + var httpContentHeaders = response.Content.Headers; |
| 181 | + |
| 182 | + if (httpContentHeaders.ContentDisposition == null) |
| 183 | + { |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + // {Last-Modified: Mon, 30 Dec 2024 09:27:04 GMT |
| 188 | + // Content-Type: application/octet-stream |
| 189 | + // Content-Disposition: attachment; filename*="UTF-8''BaiduNetdisk_txgj1_7.50.0.132.exe" |
| 190 | + // Content-Length: 403338840 |
| 191 | + // } |
| 192 | + // 这里拿到的 nameValueHeaderValue 可能就取出 |
| 193 | + NameValueHeaderValue? nameValueHeaderValue = |
| 194 | + httpContentHeaders.ContentDisposition.Parameters.FirstOrDefault(t => t.Name == "filename*"); |
| 195 | + var fileNameValue = nameValueHeaderValue?.Value; |
| 196 | + if (!string.IsNullOrEmpty(fileNameValue)) |
| 197 | + { |
| 198 | + // 额外判断一下 UTF-8 存在的情况 |
| 199 | + var match = Regex.Match(fileNameValue, @"([\S\s]*)''([\S\s]*)"); |
| 200 | + if (match.Success) |
| 201 | + { |
| 202 | + var encodingText = match.Groups[1].Value; |
| 203 | + var fileNameText = match.Groups[2].Value; |
| 204 | + if (string.Equals("utf-8", encodingText, StringComparison.OrdinalIgnoreCase)) |
| 205 | + { |
| 206 | + // 以下转码用于防止中文名乱码 |
| 207 | + var unescapeDataString = Uri.UnescapeDataString(fileNameText); |
| 208 | + return unescapeDataString; |
| 209 | + } |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + // 匹配不上,那就应该是整个都是文件名了 |
| 214 | + return fileNameValue; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + var fileName = httpContentHeaders.ContentDisposition.FileName; |
| 219 | + if (!string.IsNullOrEmpty(fileName)) |
| 220 | + { |
| 221 | + return fileName; |
| 222 | + } |
| 223 | + |
171 | 224 | if (response.Headers.TryGetValues("Content-Disposition", out var contentDispositionTextEnumerable) && contentDispositionTextEnumerable.FirstOrDefault() is { } contentDispositionText) |
172 | 225 | { |
173 | | - ServerSuggestionFileName = |
| 226 | + // 正常不会放在这里的,都是在 Content 的 Header 里面的 |
| 227 | + return |
174 | 228 | WebResponseHelper.GetFileNameFromContentDispositionText(contentDispositionText); |
175 | 229 | } |
176 | | - } |
177 | 230 |
|
178 | | - return response; |
| 231 | + return null; |
| 232 | + } |
179 | 233 | } |
180 | 234 | } |
181 | 235 | #else |
|
0 commit comments