使用 HttpClient 的简单 API Post

问题描述 投票:0回答:1

我正在尝试使用他们的 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
发送数据。他们的文档说:

“所有私有 API 调用都需要身份验证。您需要提供 3 个参数来验证请求:用户 ID、API 密钥、密码”

不知道我做错了什么, 我应该如何在请求中发送其他额外参数?

c# post dotnet-httpclient
1个回答
0
投票

请注意,代码实际上是正确的,我的各种 ID 和密码有误。

© www.soinside.com 2019 - 2024. All rights reserved.