SendAsync()返回没有数据。

huangapple go评论68阅读模式
英文:

SendAsync() returns no data

问题

I made a form in Blazor WebApplication (Client). When submited, all data gets posted to a Server, where it gets processed. The server then sends a response to the client whether the operation was successful or not, but the SendAsync() method does not return any response from the server.

string json = <Serialized Object>;

var request = new HttpRequestMessage(HttpMethod.Post, "https://<server_address>");
request.Content = new StringContent(json);
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);

using (var client = new HttpClient())
{
var response = await client.SendAsync(request).ConfigureAwait(false);
bool isSuccessCode = response.IsSuccessStatusCode; // false

string responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Console.WriteLine(responseStr); // prints empty string

if (responseStr == &quot;Success&quot;)
    return true;
else
    return false;

}

The response variable does not contain any useful data, but only has default values in it (0s and nulls).

When I tried the same code(methods) with the same config (except CORS) in a console app, it worked without any problems and I got the desired response from the server.

// This worked
internal static async Task<string> Post(string url)
{
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(data);

using var client = new HttpClient();

var result = await client.SendAsync(request).ConfigureAwait(false);

return await result.Content.ReadAsStringAsync().ConfigureAwait(false); // proper response was returned

}

Link to a GitHub project showing my issue: https://github.com/SjamesE/SendAsync-BugReport/

英文:

I made a form in Blazor WebApplication (Client). When submited, all data gets posted to a Server, where it gets processed. The server then sends a response to the client whether the operation was successful or not, but the SendAsync() method does not return any response from the server.

string json = &lt;Serialized Object&gt;;

var request = new HttpRequestMessage(HttpMethod.Post, &quot;https://&lt;server_address&gt;&quot;);
request.Content = new StringContent(json);
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);

using (var client = new HttpClient())
{
    var response = await client.SendAsync(request).ConfigureAwait(false);
    bool isSuccessCode = response.IsSuccessStatusCode; // false

    string responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
    Console.WriteLine(responseStr); // prints empty string

    if (responseStr == &quot;Success&quot;)
        return true;
    else
        return false;
}

The response variable does not contain any useful data, but only has default values in it (0s and nulls) 1 2

When I tried the same code(methods) with the same config (except CORS) in a console app, it worked without any problems and I got the desired response from the server.

// This worked
internal static async Task&lt;string&gt; Post(string url)
{
    var request = new HttpRequestMessage(HttpMethod.Post, url);
    request.Content = new StringContent(data);

    using var client = new HttpClient();

    var result = await client.SendAsync(request).ConfigureAwait(false);

    return await result.Content.ReadAsStringAsync().ConfigureAwait(false); // proper response was returned
}

Codedump to my code

Link to a GitHub project showing my issue: https://github.com/SjamesE/SendAsync-BugReport/

答案1

得分: -2

对于无意中发现这篇帖子的任何人:您不能在Blazor WebAssembly应用程序上进行无跨域请求。当使用以下代码行时,您可以发送请求,但响应不会返回到您的代码。

request.SetBrowserRequestMode(BrowserRequestMode.NoCors);

如果您仍然需要使用这些请求的响应,您需要切换到Blazor Server应用程序,或者创建一个主机并通过它路由您的请求。

英文:

For anyone stumbling upon this post: You can NOT have no-cors reqests on the Blazor WebAssembly App. When using the following line, you can send a request, but the response will not be returned to your code.

request.SetBrowserRequestMode(BrowserRequestMode.NoCors);

If you still need to use the response from those requests, you need to switch to a Blazor Server App, or to create a host and route your requests through it.

huangapple
  • 本文由 发表于 2023年6月29日 18:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定