Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#HttpClient在Get Request for Cryptsy中添加签名请求头_C#_Windows_C# 4.0_Desktop Application_Dotnet Httpclient - Fatal编程技术网

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

如何使用C#HttpClient在Get Request for Cryptsy中添加签名请求头,c#,windows,c#-4.0,desktop-application,dotnet-httpclient,C#,Windows,C# 4.0,Desktop Application,Dotnet Httpclient,我写了一个从Cryptsy.com提取硬币的程序。该程序首先将所有参数转换为HMAC SHA512,然后必须将该数据作为签名信息发送以进行身份验证。此签名数据需要以标头形式发送。当我运行这个程序时,它总是显示错误“必须进行身份验证”。我在RestClient上运行了API,但没有发现此错误。所以我想知道为什么它不能与下面的代码一起工作 client = new HttpClient(); client.BaseAddress = new Uri("https://api.cryp

我写了一个从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());
        }`
client=newhttpclient();
client.BaseAddress=新Uri(“https://api.cryptsy.com/");
int nonce=DateTime.Now.毫秒;
字符串msg=“nonce=“+nonce+”&limit=100”;
msg=msg+“&quantity=“+\u amount+”&address=“+\u with\u to+”¬ificationtoken=“+\u notification\u token;
编码=Encoding.UTF8;
HMACSHA512 hmac=新的HMACSHA512(encoding.GetBytes(_pri_key));
byte[]getsigndata=hmac.ComputeHash(encoding.GetBytes(msg));
字符串sbinary=“”;
for(int i=0;il.name===_和_from.id+“?”+消息);
requestmessage.Headers.Add(“key”,_pub_key);
requestmessage.Headers.Add(“sign”,sbinary);
HttpResponseMessage response=client.SendAsync(requestmessage).Result;
if(响应。IsSuccessStatusCode)
{
HttpContent=response.content;
String reqdata=content.ReadAsStringAsync().Result;
JObject obj=JObject.Parse(reqdata);
字符串value=obj[“success”].ToString();
布尔不成功;
如果(值=“假”)
{
isSuccess=false;
}
其他的
{
isSuccess=true;
}
如果(isSuccess)
{
}
其他的
{
字符串[]err=JsonConvert.DeserializeObject(obj[“error”].ToString());
MessageBox.Show(err[0]);
}
}
其他的
{
Show(response.StatusCode.ToString());
}`

您可以通过Fiddler或Runscope等工具运行您的工作版本和此版本,然后比较请求,看看有什么不同。我看不出您使用HttpClient有什么问题。最有可能的是,在创建符号头值的方式上存在一些细微的差异。我曾经在Visual Studio中一次又一次地调试代码,并且一直在Rest客户端中调试和运行时收集数据。在Rest中,客户端为相同的值找到了正确的结果。您可以通过Fiddler或Runscope之类的工具运行您的工作版本和此版本,然后比较请求,看看有什么不同。我看不出您使用HttpClient有任何错误。最有可能的是,在创建符号头值的方式上存在一些细微的差异。我曾经在Visual Studio中一次又一次地调试代码,并且一直在Rest客户端中调试和运行时收集数据。在Rest中,客户端为相同的值找到了正确的结果。