2017-06-01 73 views
0

我必須從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和轉讓這個,因爲它是什麼?

+0

你是否使用response.IsSuccessStatusCode檢查了響應狀態? – Saravanan

+0

是的,它的狀態代碼= 200. –

+0

你能解析來自REST Api調用的響應,並得到你想要的對象嗎?即數據是否按照您的需要返回,以便我們可以隔離問題,無論您得到的回覆是否正確,或者在發送回覆時是否存在問題。 – Saravanan

回答

1

如果這些調用是在Web API中執行的,並且沒有將邏輯應用於JSON對象,那麼在返回它之前幾乎不需要解析它,因爲它在返回時會再次被序列化,您可以而是將其解析到前端應用程序並在那裏執行您的邏輯。

HttpClient有一個方法返回響應正文作爲字符串,這是GetStringAsync。隨着身體返回字符串,你可以直接在你的HttpResponseMessage

這裏是一個使用HttpClient評論代碼的例子。

[Route("{id}")] 
public async Task<HttpResponseMessage> Get(string id) 
{ 
    using (var client = new HttpClient()) 
    { 
     client.BaseAddress = new Uri($"https://....../v1/accounts/"); 
     client.DefaultRequestHeaders.Accept.Clear(); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     // Fetch the response body as a string 
     // Resource URI added below 
     string responseContent = await client.GetStringAsync($"{id}/summary"); 

     // Create our response object and set the content to its StringContent 
     var response = 
      new HttpResponseMessage(HttpStatusCode.OK) {Content = new StringContent(responseContent)}; 

     // Return our HttpResponseMessage containing our json text 
     return response; 
    } 
} 
+1

對不起,我遲到了,但是{Content = new StringContent(responseContent)}'是重發ResponseMessage的關鍵。 –

1

如果你只想把另一API JSON響應,你可以使用類似的代碼在網頁API如下:

[HttpGet] 
    [Route("v1/test", Name = "test")] 
    public HttpResponseMessage GetTest() 
    { 
     UriBuilder uriBuilder = new UriBuilder($"https://...../..."); 
     var webRequest = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri); 
     webRequest.Method = "GET"; 
     webRequest.ContentType = "application/json; charset=utf-8"; 
     webRequest.Accept = "application/json, text/javascript, */*"; 
     using (var jsonResponse = (HttpWebResponse) webRequest.GetResponse()) 
     { 
      var jsonStream = jsonResponse.GetResponseStream(); 

      MemoryStream ms = new MemoryStream(); 
      jsonStream.CopyTo(ms); 
      ms.Position = 0; 
      HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); 
      response.Content = new StreamContent(ms); 
      response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); 
      return response; 
     }    
    } 

時所需的唯一的事情就是支持認證或授權的功能,我會更喜歡使用API​​管理器,而不是自己維護這種類型的代碼。