我想從 mvc 專案呼叫 api 操作,但我有一個問題 API 操作有兩個引數,一個在標題中,第二個在正文中。
uj5u.com熱心網友回復:
您創建一個 HttpClient 物件并設定 header 引數
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:64189/api/");
//your header parameter name and value
client.DefaultRequestHeaders.Add("hdrname", "hdrvalue");
//HTTP GET
var responseTask = client.GetAsync("youraction?param1=abc"); //Action Name
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
// Use your class model to receive the data from the API
var readTask = result.Content.ReadAsAsync<IList<YourModel>>();
readTask.Wait();
var r = readTask.Result;
}
else //web api sent error response
{
//log response status here..
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367339.html
標籤:asp.net-mvc asp.net核心 asp.net-mvc-4 asp.net-web-api
