[英]Post rest web api working from Postman but not returning any response from code in C# project
有一個POST rest api,以前可以從代碼開始工作。 但最近它已損壞,沒有返回任何響應。 但是,如果我嘗試從Postman調用api,則可以正常工作。
我可以通過什么方式調試它以找到問題的根本原因?
以下是我用來調用此帖子休息api的C#代碼
public async Task SaveToServerAsync()
{
string filePath = @"<filePath>";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
// tried this line of code from another SO answer, but this didn't work either
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://<server name>/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "d2ebf9aefbaa416adcd0");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Accept", "*/*");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(fileStream), "file", filePath);
content.Add(new StringContent("e8d002f9-f381-44c2-bce0-13416929f14d"), "Id");
try
{
var response = await client.PostAsync("<rest api end point>", content).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
Debug.Write("Response received");
}
}
catch (Exception ex)
{
Debug.Write("Exception occured");
Debug.Write(ex.Message);
}
finally
{
}
}
}
}
它總是轉到帶有異常的異常塊,因為“任務已取消”
不知道從郵遞員那里無論如何都能調試它。
因此,問題與ExpectContinue標頭有關,該標頭默認情況下為true。 服務器以某種方式無法正確處理它,並且客戶端在不確定的時間內等待繼續(100)消息。
暫時手動將此標頭設置為false可以為我們工作:
httpClient.DefaultRequestHeaders.ExpectContinue = false;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.