如何使用C#HttpClient在Get Request for Cryptsy中添加签名请求标头

时间:2015-10-07 07:04:59

标签: c# windows c#-4.0 desktop-application dotnet-httpclient

我已经编写了一个从Cryptsy.com中提取硬币的程序。该程序首先将所有参数转换为HMAC SHA512,然后必须将此数据作为身份验证的签名信息发送。此签名数据需要在标头中发送。当我运行此程序时,它始终显示错误“必须经过身份验证”。我在RestClient上运行了API,我没有发现这个错误。所以我想知道为什么它不能使用以下代码。

client = new HttpClient();
        client.BaseAddress = new Uri("https://api.cryptsy.com/");
int nonce = DateTime.Now.Millisecond;
        String msg = "nonce="+nonce+"&limit=100";
        msg = msg + "&quantity="+_amount+"&address="+_with_to+"&notificationtoken="+_notification_token;
        Encoding encoding = Encoding.UTF8;
        HMACSHA512 hmac = new HMACSHA512(encoding.GetBytes(_pri_key));
        byte[] getsigndata = hmac.ComputeHash(encoding.GetBytes(msg));
        string sbinary = "";
        for (int i = 0; i < getsigndata.Length; i++)
        {
            sbinary += getsigndata[i].ToString("X2");
        }
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
        HttpRequestMessage requestmessage = new HttpRequestMessage(HttpMethod.Get, "api/v2/withdraw/" + allCurrencies.Find(l => l.name == _with_from).id + "?" + msg);
        requestmessage.Headers.Add("key", _pub_key);
        requestmessage.Headers.Add("sign", sbinary);
        HttpResponseMessage response = client.SendAsync(requestmessage).Result;
        if (response.IsSuccessStatusCode)
        {
            HttpContent content = response.Content;
            String reqdata = content.ReadAsStringAsync().Result;
            JObject obj = JObject.Parse(reqdata);
            String valu=obj["success"].ToString();
            Boolean isSuccess;
            if (valu == "False")
            {
                isSuccess = false;
            }
            else
            {
                isSuccess = true;
            }
            if(isSuccess)
            {

            }
            else
            {
                String[] err= JsonConvert.DeserializeObject<String[]>(obj["error"].ToString());
                MessageBox.Show(err[0]);
            }
        }
        else
        {
            MessageBox.Show(response.StatusCode.ToString());
        }`

0 个答案:

没有答案