如何使用令牌授权和参数从.NET客户端调用Web API GET方法



我需要从Windows窗体应用程序调用Web API的GET方法。若要调用此Web API方法,需要令牌授权。Web API还需要一些参数。有人能帮我做这件事吗?提前感谢

[HttpGet]
[Route("api/[controller]/name={name}&email={email}phone={phone}&description={description}")]
public ActionResult<model_class> Get(string name, string email, string phone, string description) {

using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://Endpoint_or_API_URL "))
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("place_your_toke_here"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}"); 
var response = await httpClient.SendAsync(request);

HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
result = await reader.ReadToEndAsync();
}
}
}
}

最新更新