我如何向第三方api发送请求?使用httpclient

时间:2019-03-27 13:26:11

标签: c# dotnet-httpclient

当前,我需要集成CoinGecko API,这是向公众开放的免费API。 (https://www.coingecko.com/api/docs/v3

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);
            }

预期结果是它应该返回硬币列表,但永远不会返回。

1 个答案:

答案 0 :(得分:0)

将BaseUrl更改为:

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

和GetAsync调用

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