简体   繁体   中英

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. ( https://www.coingecko.com/api/docs/v3 )

The HTTP client sends the request but it never returns a response

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:

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

and the GetAsync call to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM