【问题标题】:HttpClient.SendAsync on Blazor WASM returns empty HttpResponseMessage although Fiddler shows that correct response was received尽管 Fiddler 显示收到了正确的响应,但 Blazor WASM 上的 HttpClient.SendAsync 返回空 HttpResponseMessage
【发布时间】:2021-10-25 00:03:59
【问题描述】:

我的 Blazor 页面代码:

private async Task OnSendSMSClick()
    {
        var request = new HttpRequestMessage(HttpMethod.Post, baseAddress);
        request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
        request.SetBrowserRequestCache(BrowserRequestCache.NoCache);
        HttpContent content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("client_id", client_id),
                new KeyValuePair<string, string>("client_secret", client_secret)
            });
        content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
        request.Content = content;
        RequestOutput = request.ToString();

        HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);
        
        ResponseOutput = response.StatusCode.ToString();
    }

response 总是返回空,尽管我在 Fiddler 上看到返回了实际结果。在 Google Chrome 上,我看到服务器以正确的标头响应 200,但不知何故 Chrome 显示“加载响应数据失败”。

Chrome 上没有错误,我收到 200 响应,但不知何故无法显示响应内容:

而且你可以在调试器中看到响应对象是空的..

我该如何解决这个问题,我在这里缺少什么?

谢谢!

【问题讨论】:

    标签: blazor httpclient webassembly httpresponsemessage sendasync


    【解决方案1】:

    Blazor 在需要 CORS (source) 的网站上存在问题。

    很遗憾,在 Blazor 客户端上无能为力。
    我找到的唯一解决方案是 disable CORS on the server 或在 Blazor 之外设置代理服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2021-04-08
      相关资源
      最近更新 更多