【问题标题】:Blazor Client HttpClient. No Content ReturnedBlazor 客户端 HttpClient。没有内容返回
【发布时间】:2021-08-10 06:52:47
【问题描述】:

我的 C# 代码一定有问题。我正在尝试从 Azure Blob 下载一些 Json。我可以在 Postman 和 MS Edge 中下载 Json,但是,使用我的代码,请求中没有明显的错误,但响应中没有内容。大概是我的代码有问题。

    async Task GetJson()
    {
        var request = new HttpRequestMessage
        {
            Method = new HttpMethod("GET"),
            RequestUri = new Uri("https://xxx.blob.core.windows.net/trading/m5.json")
        };
        request.Headers.Add("Accept", "application/json");
        request.SetBrowserRequestMode(BrowserRequestMode.NoCors);

        var response = await http.SendAsync(request);
        var json = await response.Content.ReadAsStringAsync();

    
    }

【问题讨论】:

    标签: blazor-webassembly


    【解决方案1】:

    这是在 GitHub 上询问的,显然是 by design

    当您删除 request.SetBrowserRequestMode(BrowserRequestMode.NoCors); 行时,您将看到 No 'Access-Control-Allow-Origin' header is present 错误。

    指定 BrowserRequestMode.NoCors 不会让您绕过浏览器安全规则。它只是简化了请求标头。

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2020-10-27
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多