SendAsync()返回没有数据。

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

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

  1. string responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
  2. Console.WriteLine(responseStr); // prints empty string
  3. if (responseStr == &quot;Success&quot;)
  4. return true;
  5. else
  6. 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);

  1. using var client = new HttpClient();
  2. var result = await client.SendAsync(request).ConfigureAwait(false);
  3. 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.

  1. string json = &lt;Serialized Object&gt;;
  2. var request = new HttpRequestMessage(HttpMethod.Post, &quot;https://&lt;server_address&gt;&quot;);
  3. request.Content = new StringContent(json);
  4. request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
  5. using (var client = new HttpClient())
  6. {
  7. var response = await client.SendAsync(request).ConfigureAwait(false);
  8. bool isSuccessCode = response.IsSuccessStatusCode; // false
  9. string responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
  10. Console.WriteLine(responseStr); // prints empty string
  11. if (responseStr == &quot;Success&quot;)
  12. return true;
  13. else
  14. return false;
  15. }

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.

  1. // This worked
  2. internal static async Task&lt;string&gt; Post(string url)
  3. {
  4. var request = new HttpRequestMessage(HttpMethod.Post, url);
  5. request.Content = new StringContent(data);
  6. using var client = new HttpClient();
  7. var result = await client.SendAsync(request).ConfigureAwait(false);
  8. return await result.Content.ReadAsStringAsync().ConfigureAwait(false); // proper response was returned
  9. }

Codedump to my code

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

答案1

得分: -2

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

  1. 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.

  1. 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:

确定