简体   繁体   English

我如何向第三方api提出请求? 使用httpclient

[英]How do i do a request to a third party api? using httpclient

Currently, I need to integrate the CoinGecko API, this is a free API open to the public. 目前,我需要集成CoinGecko API,这是一个向公众开放的免费API。 ( https://www.coingecko.com/api/docs/v3 ) https://www.coingecko.com/api/docs/v3

The HTTP client sends the request but it never returns a response HTTP客户端发送请求但它永远不会返回响应

string BaseUrl = "https://api.coingecko.com/api/v3";

 HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(BaseUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("/coins/list");
            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();
                var table = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.Coins>>(data);
            }

The expected result is that it should return the coins list, but it never does. 预期的结果是它应该返回硬币列表,但它永远不会。

Change BaseUrl to: 将BaseUrl更改为:

 string BaseUrl = "https://api.coingecko.com";

and the GetAsync call to 和GetAsync调用

HttpResponseMessage response = await client.GetAsync("/api/v3/coins/list");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用第三方控件的情况下发出SAML登录请求? - How do I do a SAML login request, without using a third party control? 如何使用新的WCF REST HttpClient API设置GoogleLogin请求的Authorization标头 - How do I do set the Authorization header of a GoogleLogin request using the new WCF REST HttpClient API 如何取消第三方任务? - How do I cancel third party task? 如何在 dotnet core 中使用 HttpClient 进行补丁请求? - How do I do a patch request using HttpClient in dotnet core? 如何保持第三方API和我自己的数据库之间的一致性? - How do I maintain consistency between third-party API and my own database? 如何在.NET中导入第三方IronPython模块? - How do I import a third-party IronPython module in .NET? 在WPF中,如何将样式应用于第三方组件 - In WPF how do I apply a style to a third party component LINQPad如何与第三方插件一起使用? - How do I use LINQPad with third party plugins? 如何通过将 HttpClient 请求转换为 RestSharp 请求来上传文档? - How do I upload document by converting HttpClient request to RestSharp request? 使用 state 通过第三方扩展拦截 HttpClient - Intercept HttpClient with third party extensions using state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM