使用HttpClient的简单API帖子

时间:2017-11-24 20:22:06

标签: c# api post httpclient

我试图通过使用他们的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说&#34;所有私有API调用都需要身份验证您需要提供3个参数来验证请求:用户ID,API密钥,密码短语&#34;

不确定我做错了什么?

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

1 个答案:

答案 0 :(得分:0)

请注意,代码实际上是正确的,我使用各种ID和密码时出错了。