我有以下 WebClient 代碼,假設我的代碼充當代理,并鏈接到遠程服務器,我想拋出從遠程服務器回傳的任何回應,我該怎么做?我不想處理例外或任何事情,只是拋出回應。
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
Uri NODE_LOGIN_PATH = new Uri(URI_Combine(NodeAPI, "auth/login"));
string jsonString = JsonConvert.SerializeObject(login_details);
JObject data = JObject.Parse(await client.UploadStringTaskAsync(NODE_LOGIN_PATH, jsonString));
return data;
}
uj5u.com熱心網友回復:
您似乎在 MVC 4 專案中使用 WebClient,所以您使用的是舊東西。考慮升級到 HttpClient 和 ASP.NET Core。
你想要的原則是這樣的:
public class FooController : ApiController
{
public HttpResponseMessage Get()
{
// Do the HTTP call
var httpResponse = client.DoSomeRequest();
// Translate
var apiResponse = new HttpResponseMessage
{
StatusCode = httpResponse.StatusCode.Map(...),
Headers = httpResponse.Headers.Map(...),
Body = httpResponse.Body.Map(...),
};
// Return
return apiResponse;
}
}
所以:執行請求,并將其轉換(映射)到您的 Web API 平臺所需的 HttpResponseMessage(或 IHttpActionResult,或 ...)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/328546.html
