如何使用令牌授权和参数从 .NET 客户端调用 Web API GET 方法
How to call a Web API GET method from a .NET client using also token authorization and parameters
我需要从 Windows Forms 应用程序调用 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();
}
}
}
}
我需要从 Windows Forms 应用程序调用 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();
}
}
}
}