![](/img/trans.png)
[英]Using WebProxy with HttpClient.SendAsync() and HttpRequestMessage
[英]Getting 304 response using Core 3.1 HttpClient.SendAsync
我在 .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);
}
304 主要是為了確保在客戶端已經具有與服務器上相同的信息表示的情況下最小化信息傳輸。 304 通常只以標頭結尾,如其他答案中所述,我還建議查看請求的標頭,給出 304 的標頭和給出 200 OK 的標頭,您可能還需要了解是否有特定的304 之后的類將成功執行 200 OK 或對服務器的每個替代調用的時間段是 304,然后是 200 OK,然后是 200 OK。 如果可能,請發布標題比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.