【发布时间】:2016-12-01 14:42:46
【问题描述】:
不久前,我使用 HttpClient 类实现了一些代码来使用 REST Api。
using (var client = new HttpClient() { BaseAddress = new Uri(@"https://thirdparty.com") })
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(...);
var uri = new Uri(@"rest/api/foo", UriKind.Relative);
var content = new StringContent(json.ToString());
using (var response = await client.PostAsync(uri, content))
{
// etc ...
}
}
这段代码似乎在测试和生产环境(每个环境都访问一个测试/生产 uri)上都可以正常工作。最近,我们开始在生产环境中得到一个HttpRequestException:System.Net.Http.HttpRequestException: Error while copying content to a stream.
这似乎有点奇怪,所以我使用 Postman 发送相同的消息,它工作得很好。我不确定为什么我们的代码失败而 Postman 正在工作。我更改了 json 数据中的一个参数(从“NY”到“NV”的状态),我们的 .NET 代码运行良好——当然我们不能只发送错误的 json 数据,所以这不是解决方案;这更像是一种观察,即完全相同的代码适用于不同的内容。
有趣的是,我们可以进行两个代码更改来解决这个问题。首先,Postman 能够使用 RestSharp 包生成工作 C# 代码。或者,我从另一个指向使用 HttpVersion 1.0 的问题中找到了 answer:
using (var request = new HttpRequestMessage(HttpMethod.Post, uri))
{
request.Version = HttpVersion.Version10;
request.Content = new StringContent(json.ToString());
using (var response = await client.SendAsync(request))
{
// etc ...
}
}
令人困惑的部分是 Postman 使用的是 HTTP/1.1 版本。所以,总结一下:
- 如果我们更改 json 数据(美国州从“NY”改为“NV”),代码就可以工作。
- 相同的 json 和代码适用于测试 uri。
- 更改代码以使用 RestSharp 包有效。
- 将代码更改为使用 HTTP/1.0 而不是 HTTP/1.1 有效。
到底为什么 Postman 能够使用 HTTP/1.1 工作,但 HttpClient 却失败了?这是我们客户的问题吗(该代码适用于美国其他州)?这是 .NET Framework 中的错误吗?第三方的 REST Api 的实现/托管是否有问题?
邮递员标题:
POST rest/api/foo HTTP/1.1
Host: thirdparty.com
Content-Type: application/json
Authorization: Basic SOME_ENCRYPTED_USER_PASS
Cache-Control: no-cache
Postman-Token: 2fa5b5a0-b5d3-bd4c-40f0-d2b55b60316b
示例 Json:
{
"stateCode": "NY",
"packageID": "58330",
"name": "58330-PRI-1",
"documents": [
{
"type": "SPECIAL",
"name": "Sample Document",
"documentID": "3569976"
}
],
"descriptions": [
{
"city": "New York",
"state": "NY"
}
]
}
堆栈跟踪:
AggregateException: One or more errors occured.
HttpRequestException: Error while copying content to a stream.
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
SocketException: An existing connection was forcibly closed by the remote host.
【问题讨论】:
-
postman 的 body 是否和使用 client.PostAsync() 发送的 body 完全一样?我相信版本之间的区别在于 keep-alive 标头,因此 Postman 处理超时的方式可能与 .NET 不同。
-
@NickSpicer:正确,正文是相同的json数据。而且我相信是 HTTP/1.1 与 HTTP/1.0 决定了连接保持活动与关闭的事实。我已经更新了我的答案以添加邮递员标题数据。
-
你一直在使用 async/await 吗?当您将 async/await 调用与其正常的同步对应调用混合时,就会发生奇怪的事情。
-
当您更改数据时它起作用的事实似乎证明服务器代码存在问题。我认为这与 HTTP 版本(1.0 与 1.1)无关,或者您有一个非常奇怪的服务器破坏了 HTTP 级别。
-
请将异常的完整堆栈跟踪添加到您的问题中。可能有我有用的内部异常。
标签: c# json rest dotnet-httpclient postman