【问题标题】:Execute PostAsync with empty response使用空响应执行 PostAsync
【发布时间】:2019-08-14 16:57:31
【问题描述】:

对于我的带有 MS Store 集成的 UPW 应用程序,我的 ASP.NET 应用程序对 Microsoft Store 集合 API 进行了一些调用:

https://docs.microsoft.com/de-de/windows/uwp/monetize/report-consumable-products-as-fulfilled

这个API方法根本没有返回:

"响应: 消费成功则不返回任何内容。”

这导致了我的以下代码:

HttpClient httpClient = new HttpClient();
String content = "...";
var res = httpClient.PostAsync("https://collections.mp.microsoft.com/v6.0/collections/consume", content).Result;

抛出异常。不幸的是,我只有德语版本,但它表明无法从连接中读取数据,并且远程主机已关闭连接。

我认为这基本上是一个“功能”而不是错误,因为 真的没有内容可以返回。但我不知道如何摆脱异常。

例外: System.Net.Http.HttpRequestException: Fehler beim Senden der Anforderung。 ---> System.Net.WebException: Die zugrunde liegende Verbindung wurde geschlossen: Unbekannter Fehler beim Empfangen.. ---> System.IO.IOException: Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen。 ---> System.Net.Sockets.SocketException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen bei System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state) bei System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) --- Ende der internen Ausnahmestapelüberwachung --- 北 System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult) ...

【问题讨论】:

  • 顺便说一句,您不应该使用返回任务的 Result 属性,而是等待 PostAsync 方法。

标签: c# asp.net asynchronous


【解决方案1】:

这是网络错误。

文档(this 是英文版)确实说:

消费成功则不返回任何内容。

不过,它也给出了example of a response

HTTP/1.1 204 No Content
Content-Length: 0
MS-CorrelationId: 386f733d-bc66-4bf9-9b6f-a1ad417f97f0
MS-RequestId: e488cd0a-9fb6-4c2c-bb77-e5100d3c15b1
MS-CV: 5.1
MS-ServerId: 030011326
Date: Tue, 22 Sep 2015 20:40:55 GMT

所以它确实会响应,但会使用HTTP 204

如果您没有收到该响应,则说明您有网络问题。

【讨论】:

  • 很遗憾没有。它可能会响应 204,但上面提到的代码行会引发异常。这绝对不是网络错误。我知道这是因为:在抛出异常并且我再次请求之后,该项目真的被购买了。这意味着,呼叫通过并被执行。但是还是抛出了异常。
  • 所以是的,请求已被处理,但错误是在您获得响应之前网络连接已关闭。那不应该发生。如果一切正常,应该没有例外。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
相关资源
最近更新 更多