【问题标题】:Simple API Post with HttpClient使用 HttpClient 的简单 API 发布
【发布时间】:2017-11-24 20:22:05
【问题描述】:

我正在尝试使用他们的 API 从 coinfloor 获得平衡, 虽然我能够成功解析他们未经身份验证/公开的 GET 响应,但在发送带有额外参数的经过身份验证的私有 POST 请求时遇到了麻烦。

目前我得到 StatusCode: 401, ReasonPhrase: 'Unauthorized'

  class Program
    {
        static HttpClient client = new HttpClient();

    static void Main()
        {
            RunAsync().Wait();
        }

        static async Task RunAsync()
        {
            client.BaseAddress = new Uri("https://webapi.coinfloor.co.uk:8090/bist/XBT/GBP/balance/");
            client.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "userID/apikey", "passphrase"))));

            var content = new FormUrlEncodedContent(new[]
                    {
                       new KeyValuePair<string, string>("User ID","userid"),
                       new KeyValuePair<string, string>("API key","apikey"),
                       new KeyValuePair<string, string>("Passphrase","pass")

                    });
            try
            {

                   var result = await client.PostAsync("https://webapi.coinfloor.co.uk:8090/bist/XBT/GBP/balance/", content);
                   string resultContent = await result.Content.ReadAsStringAsync();

             }

我已尝试使用默认授权标头并使用 FormUrlEncodedContent 发送数据。他们的documentation 说“所有私有 API 调用都需要身份验证。您需要提供 3 个参数来验证请求:用户 ID、API 密钥、密码”

不确定我做错了什么?

我应该如何在请求中发送其他额外参数?

【问题讨论】:

  • 在 fiddler 中查看您的请求并确保它看起来像预期的那样不会有什么坏处。
  • 您发送的是用户 ID、api 密钥和密码短语的实际值还是字符串值“userID/apikey”和“密码短语”?
  • 感谢 Tobias 和 Evk,我在代码中确实有正确的值,只是为这篇文章编辑。试图弄清楚正确的请求应该是什么样子,在 fiddler 中的请求标头下,我看到标准标头加上一个附加字符串,其中包含来自 FormUrlEncodedContent 的值,这对我来说似乎是正确的

标签: c# api post httpclient


【解决方案1】:

只是说明代码实际上是正确的,我的各种 ID 和密码有误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    相关资源
    最近更新 更多