我必須從Web API調用REST服務。 調用和檢索數據運行良好,但返回方法崩潰。 我試過幾種方法,返回async HttpResponseMessage
,但我也可以返回這個對象(錯誤套接字在鉻合金ERR_SPDY_PROTOCOL_ERROR)。 我已經試過,只是一個普通的json作爲字符串,但沒有更多的成功。從WebAPi調用REST服務並傳輸HttpResponseMessage
這裏是一些嘗試:
[Route("{id}")]
public async Task<JObject> Get(string id)
{
dynamic client = new RestClient($"https://...../accounts/{id}/summary",
new Dictionary<string, string> {
//some KVP for access Rest API
});
//await client.Get() returns HttpResponseMessage 200 and Content is well populated
JObject result = JObject.FromObject(await client.Get());
return result;
//Request.CreateResponse(HttpStatusCode.OK, await result.Content.ReadAsAsync<string>());
//HttpResponseMessage result = await client.Get(); => HttpResponseMessage is well filled
//Request.CreateResponse(HttpStatusCode.OK, await result.Content.ReadAsAsync<string>()); => test with wrapping inside a new HttpResponseMessage but no more success
//using (var client = new HttpClient())
//{
// client.BaseAddress = new Uri($"https://....../v1/accounts/{id}/summary");
// client.DefaultRequestHeaders.Accept.Clear();
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HttpResponseMessage response = await client.GetAsync("");
// return Request.CreateResponse(HttpStatusCode.OK, response.Content.ReadAsAsync());
//}
}
有一個簡單的方法來從REST服務檢索JSON和轉讓這個,因爲它是什麼?
你是否使用response.IsSuccessStatusCode檢查了響應狀態? – Saravanan
是的,它的狀態代碼= 200. –
你能解析來自REST Api調用的響應,並得到你想要的對象嗎?即數據是否按照您的需要返回,以便我們可以隔離問題,無論您得到的回覆是否正確,或者在發送回覆時是否存在問題。 – Saravanan