You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中GetAsync获取带Refresh Meta标签页面返回空,如何提取下载链接?

解决方案

1. 修复页面内容读取问题

原代码返回空字符串,大概率是编码不匹配或未正确读取响应流。可以用ReadAsStringAsync()简化读取逻辑,HttpClient会自动处理响应编码:

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.GetAsync("https://petra.assurance-maladie.fr/fs/index.PnS?reqid=230620085933huRgfH9fPQwyDDqpJTw2B7d997a").ConfigureAwait(false);
    // 确保请求成功,否则抛出异常
    response.EnsureSuccessStatusCode();
    // 直接读取响应内容为字符串
    string htmlContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
    return htmlContent;
}

2. 提取Meta Refresh中的真实下载链接

推荐使用HtmlAgilityPack(通过NuGet安装)解析HTML,比正则表达式更稳定可靠:

步骤:

  1. 安装NuGet包:Install-Package HtmlAgilityPack
  2. 解析HTML并提取目标链接:
using HtmlAgilityPack;

public async Task<string> GetRealDownloadUrl()
{
    using (HttpClient client = new HttpClient())
    {
        string baseUrl = "https://petra.assurance-maladie.fr/fs/index.PnS?reqid=230620085933huRgfH9fPQwyDDqpJTw2B7d997a";
        HttpResponseMessage response = await client.GetAsync(baseUrl).ConfigureAwait(false);
        response.EnsureSuccessStatusCode();
        string html = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);

        // 定位http-equiv为refresh的meta标签
        var metaNode = doc.DocumentNode.SelectSingleNode("//meta[@http-equiv='refresh']");
        if (metaNode == null)
            throw new InvalidOperationException("未找到Refresh Meta标签");

        string content = metaNode.GetAttributeValue("content", string.Empty);
        if (string.IsNullOrEmpty(content))
            throw new InvalidOperationException("Meta标签Content属性为空");

        // 拆分content内容,提取URL部分
        var urlPart = content.Split(new[] { "url=" }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
        if (string.IsNullOrEmpty(urlPart))
            throw new InvalidOperationException("无法从Content中提取下载链接");

        // 拼接完整绝对URL
        Uri baseUri = new Uri(baseUrl);
        return new Uri(baseUri, urlPart).ToString();
    }
}

如果不想依赖第三方库,也可以用正则表达式(注意HTML结构变化可能导致失效):

using System.Text.RegularExpressions;

// 在获取htmlContent之后执行
Regex refreshRegex = new Regex(@"meta http-equiv=""refresh"" content=""\d+; url=(.*?)""", RegexOptions.IgnoreCase);
Match match = refreshRegex.Match(htmlContent);
if (match.Success)
{
    string relativeUrl = match.Groups[1].Value;
    Uri baseUri = new Uri("https://petra.assurance-maladie.fr/fs/index.PnS?reqid=230620085933huRgfH9fPQwyDDqpJTw2B7d997a");
    string realDownloadUrl = new Uri(baseUri, relativeUrl).ToString();
}

3. 下载ZIP文件

拿到真实链接后,直接用HttpClient下载并保存到本地:

public async Task DownloadZipFile(string realDownloadUrl, string saveFilePath)
{
    using (HttpClient client = new HttpClient())
    {
        HttpResponseMessage response = await client.GetAsync(realDownloadUrl).ConfigureAwait(false);
        response.EnsureSuccessStatusCode();
        
        using (Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
        using (FileStream fileStream = new FileStream(saveFilePath, FileMode.Create, FileAccess.Write))
        {
            await responseStream.CopyToAsync(fileStream).ConfigureAwait(false);
        }
    }
}

整合调用示例

public async Task RunDownload()
{
    try
    {
        string realUrl = await GetRealDownloadUrl().ConfigureAwait(false);
        await DownloadZipFile(realUrl, @"C:\Your\Save\Path\file.zip").ConfigureAwait(false);
        Console.WriteLine("ZIP文件下载完成");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"下载失败:{ex.Message}");
    }
}

内容的提问来源于stack exchange,提问作者Seb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 17:52:49