使用HttpClient调用HTTPS GET



我得到一个400坏的请求,试图使用HttpClient调用API,我不完全确定我错过了什么。

我已经从浏览器和邮差尝试了端点,没有任何问题,但是当我尝试使用以下代码时,我得到了一个400错误的请求:

using (HttpClient client = new HttpClient())
{
var result = client.GetAsync("https://api.exchange.coinbase.com/products/ETH-USD/candles?granularity=300&start=2022-03-22&end=2022-03-23").Result;
}

有没有人能告诉我我错过了什么让这个工作?

原始代码的请求返回{"message":"User-Agent header is required."}。如果是这种情况,添加user-agent报头来修复这个问题

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using (HttpClient client = new HttpClient())
{   
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(".NET", "6.0"));
var result = await client.GetAsync("https://api.exchange.coinbase.com/products/ETH-USD/candles?granularity=300&start=2022-03-22&end=2022-03-23");   
Console.WriteLine(await result.Content.ReadAsStringAsync());
}

相关内容

最新更新