【发布时间】:2020-03-04 01:29:04
【问题描述】:
我使用 .Net Framework 4.6.1 下的 HttpClient.SendAsync() 将 JSON 发布到供应商的服务器,并毫无问题地获得有效响应。但是,当我在 Core 3.1.2 下尝试相同的代码时,我在第一次尝试时获得了 304(未修改)的 HTTP 状态代码,但在第二次尝试时获得了良好的响应。 Core 发生了什么变化?有关如何解决此问题的任何建议?
private static readonly HttpClient httpClient = new HttpClient();
public static async Task<HttpResponseMessage> PostJson(string uri, string userName, string password, object content)
{
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
AddBasicAuthorizationHeader(userName, password, httpRequestMessage);
httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
return await httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
}
【问题讨论】:
-
您是否准确比较了每个版本之间的请求发送的标头/值?
标签: c# .net-core dotnet-httpclient