【问题标题】:How to call a Web API GET method from a .NET client using also token authorization and parameters如何使用令牌授权和参数从 .NET 客户端调用 Web API GET 方法
【发布时间】:2020-08-18 10:05:06
【问题描述】:

我需要从 Windows 窗体应用程序调用 Web API 的 GET 方法。 要调用此 Web API 方法,需要令牌授权。 Web API 还需要一些参数。 有谁可以帮助我如何做到这一点? 提前致谢

【问题讨论】:

标签: c# winforms client webapi


【解决方案1】:
[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();
                        }
            }
        }
        }

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2020-12-16
    • 2017-08-04
    相关资源
    最近更新 更多