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

  1. Provide a fully-qualified URL starting with http:// or https:// (e.g. https://example.com/bg.png).
  2. Trim whitespace and validate the URL in the UI before calling DownloadAndSaveAsync.
  3. If users may omit the scheme, prepend 'https://' client-side before validation.
  4. 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

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


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/f5c6583a3561be58. Report an issue: GitHub.