babalae/better-genshin-impact · error · ArgumentException
请输入有效的 HTTP/HTTPS 图片地址。
Error message
请输入有效的 HTTP/HTTPS 图片地址。
What it means
An ArgumentException thrown by BannerImageService.DownloadAndSaveAsync when the supplied url is not an absolute URI or its scheme is neither http nor https. Uri.TryCreate with UriKind.Absolute must succeed and the scheme is checked against the standard http/https schemes; anything else (ftp, file, relative path, malformed string) is rejected before any network call.
Source
Thrown at BetterGenshinImpact/Service/BannerImageService.cs:69
{
File.WriteAllText(tempPath, url, new UTF8Encoding(false));
lock (_fileCommitLock)
{
File.Move(tempPath, UrlConfigPath, true);
}
}
finally
{
TryDeleteFile(tempPath);
}
}
public async Task<bool> DownloadAndSaveAsync(string url, CancellationToken cancellationToken = default)
{
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)
|| (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps))
{
throw new ArgumentException("请输入有效的 HTTP/HTTPS 图片地址。", nameof(url));
}
var operationId = Interlocked.Increment(ref _latestOperationId);
var directory = Path.GetDirectoryName(NetworkImagePath)
?? throw new InvalidOperationException("无法确定网络背景图片目录。");
Directory.CreateDirectory(directory);
var tempPath = $"{NetworkImagePath}.{Guid.NewGuid():N}.tmp";
using var timeoutCancellationTokenSource = new CancellationTokenSource(DownloadTimeout);
using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken,
timeoutCancellationTokenSource.Token);
var linkedCancellationToken = linkedCancellationTokenSource.Token;
try
{
// 下载图片
using var response = await _httpClient.GetAsync(View on GitHub (pinned to a7cb36712d)
Solutions
- Provide a fully-qualified URL starting with http:// or https:// (e.g. https://example.com/bg.png).
- Trim whitespace and validate the URL in the UI before calling DownloadAndSaveAsync.
- If users may omit the scheme, prepend 'https://' client-side before validation.
- Catch ArgumentException and show a field-level validation message to the user.
Example fix
// before
await _banner.DownloadAndSaveAsync(userInput);
// after
var url = userInput.Trim();
if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
&& !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
url = "https://" + url;
}
if (!Uri.TryCreate(url, UriKind.Absolute, out _))
{
Toast.Warning("请输入有效的 HTTP/HTTPS 图片地址。");
return;
}
await _banner.DownloadAndSaveAsync(url); Defensive patterns
Strategy: validation
Validate before calling
if (!Uri.TryCreate(url?.Trim(), UriKind.Absolute, out var uri)
|| (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps))
{
Toast.Warning("请输入有效的 HTTP/HTTPS 图片地址。");
return;
} Type guard
static bool IsAbsoluteHttpUrl(string s) => Uri.TryCreate(s, UriKind.Absolute, out var u) && (u.Scheme == Uri.UriSchemeHttp || u.Scheme == Uri.UriSchemeHttps);
Try / catch
try { await _banner.DownloadAndSaveAsync(url, ct); }
catch (ArgumentException ex) { Toast.Warning(ex.Message); } Prevention
- Validate and trim the URL in the UI before calling.
- Prepend 'https://' when the user omits the scheme.
- Show a field-level validation message on ArgumentException.
When it happens
Trigger: Passing a relative URL, a file path, an ftp/data URI, or a malformed string (missing scheme/host) to DownloadAndSaveAsync. Also a url that is null or whitespace where TryCreate fails.
Common situations: User pastes a bare path or a CDN link missing 'https://'; a config/setting stored the URL without scheme; clipboard content with leading spaces or a wrapped link; an ftp:// or file:// link mistaken for an image URL.
Related errors
- URL不能为空
- 策略字符串不能为空
- 无效的小时值: {hour},必须是 0-24 之间的整数字符
- 无效的分钟值: {minute},必须是 0-59 之间的整数字符
- requestId cannot be empty
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/f5c6583a3561be58.
Report an issue: GitHub.