C# 如何使用令牌授权和参数从.NET客户端调用Web API GET方法
我需要从Windows窗体应用程序调用Web API的GET方法。 要调用此Web API方法,需要令牌授权。 Web API还需要一些参数。 有人能帮我做这件事吗? 提前感谢C# 如何使用令牌授权和参数从.NET客户端调用Web API GET方法,c#,winforms,client,webapi,C#,Winforms,Client,Webapi,我需要从Windows窗体应用程序调用Web API的GET方法。 要调用此Web API方法,需要令牌授权。 Web API还需要一些参数。 有人能帮我做这件事吗? 提前感谢[HttpGet] [HttpGet] [Route("api/[controller]/name={name}&email={email}phone={phone}&description={description}")] public ActionResult<
[HttpGet]
[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();
}
}
}
}
[路由(“api/[controller]/name={name}&email={email}phone={phone}&description={description}”)]
获取公共操作结果(字符串名称、字符串电子邮件、字符串电话、字符串描述){
使用(var httpClient=new httpClient())
{
使用(var request=newhttprequestmessage(newhttpmethod(“GET”),”https://Endpoint_or_API_URL "))
{
var base64authorization=Convert.ToBase64String(Encoding.ASCII.GetBytes(“将您的toke放在这里”);
request.Headers.TryAddWithoutValidation(“Authorization”,$“Basic{base64authorization}”);
var response=wait httpClient.sendaync(请求);
HttpContent responseContent=response.Content;
使用(var reader=new StreamReader(wait responseContent.ReadAsStreamAsync()))
{
结果=等待读卡器。ReadToEndAsync();
}
}
}
}
尝试将类与