https使用httpclient呼叫

发布于 2025-02-04 05:04:01 字数 395 浏览 2 评论 0原文

我收到了一个400不好的请求,试图使用httpclient调用API,但我不确定我缺少什么。

我尝试了浏览器和Postman的终点,但没有任何问题,但是当我尝试使用以下代码时,我会收到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;
}

有人可以告诉我,我缺少什么来让这个工作来工作?

I am getting a 400 bad request trying to call an API using HttpClient and I am not exactly sure what I am missing.

I have tried the endpoint from my browser and from Postman and have not had any issues, but when I try to use the following code I get a 400 bad request:

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

Would anyone be able to tell me what I am missing to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2025-02-11 05:04:01

原始代码中的请求返回{“ message”:“需要用户代理标头。”}。如果是这种情况,请添加用户代理标头来解决问题

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

The request from the original code returns {"message":"User-Agent header is required."}. If it is the case, add user-agent header to fix the problem

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文