我有這個麻煩,我不是爲什麼,它一定是我做錯了什麼。我必須重寫才能使其工作,但它聞起來都是錯誤的,但它的工作原理。WebApi - PostAsync
所以這是我第一次嘗試,並沒有工作,因爲被退回的500的StatusCode,但這是因爲它不是在等待響應,我需要等待
[HttpPost]
public async Task<JsonResult> Booking(string model)
{
//do some bits.
var a = new JavaScriptSerializer().Serialize(e);
var booking = new HttpClient();
HttpContent content = new StringContent(a,Encoding.UTF8);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await booking.PostAsync("https://webapi.domain.com/Booking/Post", content);
var aa = response.StatusCode //500 Internal Error
}
所以我改寫了
[HttpPost]
public async Task<JsonResult> Booking(string model)
{
//do some bits.
var a = new JavaScriptSerializer().Serialize(e);
var booking = new HttpClient();
HttpContent content = new StringContent(a,Encoding.UTF8);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await booking.PostAsync("https://webapi.domain.com/Booking/Post", content);
var t = new Stopwatch();
while (response.StatusCode ==HttpStatusCode.InternalServerError)
{
t.Start();
var zzzz = response.ReasonPhrase;
if (t.ElapsedMilliseconds >10000)
{
response.StatusCode = HttpStatusCode.RequestTimeout;
t.Stop();
}
}
var aa = response.StatusCode //201 Created
}
而這個工作並返回我,我的201的,醜陋的,但任何人都可以告訴我,告訴我什麼,我做錯了什麼?
請格式化代碼! –
什麼是'booking.PostAsync'?你爲什麼不直接去那裏? –
我發現當第一個代碼不工作時,第二塊代碼不太可能工作。你確定?響應狀態代碼在一段時間後不會改變。 HTTP協議無法「更新」已發送的響應。所有這些計時的東西什麼都不做 – usr