請試試我可能無法使HttpClient.PutAsync方法正常工作。我能夠使GetAsync方法正常工作。我也能夠在郵遞員中成功完成PUT呼叫HttpClient PutAsync掛起問題或404返回
這是我使用的代碼,當我找不到404時。
public static async Task<HttpResponseMessage> PutAsJsonAsync<Contact>(HttpClient client, Contact contact)
{
client.DefaultRequestHeaders.Add("X-API-TOKEN","MyT0k3n");
var json = JsonConvert.SerializeObject(contact);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
return await client.PutAsync("https://survey.qualtrics.com/API/v3/mailinglists/ML_123456/contacts/MLRP_123456", stringContent);
}
Contact類具有諸如「unsubscribed」之類的變量。
我也試過這個(https://gist.github.com/alexandrevicenzi/9216739),但是當我等待任務的線程掛起和超時:
public static Task<HttpResponseMessage> PutJsonAsync(HttpClient client, string requestUri, Type type, object value)
{
return client.PutAsync(requestUri, new ObjectContent(type, value, new JsonMediaTypeFormatter(), "application/json"));
}
我說實話不是很熟悉異步,等待,或HttpClient的。我過去使用過HttpWebRequest和WebRequest。我試圖用我的舊方法PUT,並沒有成功。有人有主意嗎?
通過提琴手發送這兩個請求,並告訴我們結果。我猜你的請求沒有格式化 - > 404。 – burnsi
謝謝@burnsi !!這是相當尷尬的..我應該爲https配置提琴手,然後在問這裏之前嘗試。我的請求URI缺少「/」。所以很自然。我太專注於語法,錯過了錯字。我很感激幫助,您的評論是正確的,並引導我找到我的問題。如果您將它作爲答案發布,我會接受它。 – hackingchemist