问题描述
请帮我处理 C# 中的 POST api 请求.我不知道如何在 POST 请求中正确发送参数key"、signature"和nonce".它不断告诉我缺少密钥、签名和随机数参数".
please, help me with POST api request in C#.I dont know how to correctly send parameters „key", „signature" and „nonce" in POST request. It constantly tells me "Missing key, signature and nonce parameters".
HttpWebRequest webRequest =(HttpWebRequest)System.Net.WebRequest.Create("https://www.bitstamp.net/api/balance/");
if (webRequest != null)
{
webRequest.Method = HttpMethod.Post;
webRequest.ContentType = "application/json";
webRequest.UserAgent = "BitstampBot";
byte[] data = Convert.FromBase64String(apisecret);
string nonce = GetNonce().ToString();
var prehash = nonce + custID + apikey;
string signature = HashString(prehash, data);
body = Serialize(new
{
key=apikey,
signature=signature,
nonce=nonce
});
if (!string.IsNullOrEmpty(body))
{
var data1 = Encoding.UTF8.GetBytes(body);
webRequest.ContentLength = data1.Length;
using (var stream = webRequest.GetRequestStream()) stream.Write(data1, 0, data1.Length);
}
using (Stream s = webRequest.GetResponse().GetResponseStream())
{
using (StreamReader sr = new System.IO.StreamReader(s))
{
contentBody = await sr.ReadToEndAsync();
return contentBody;
}
}
}
推荐答案
Bitstamp 在 docs 实际上应该以内容类型application/x-www-form-urlencoded"而不是application/json"发送.
The "Request parameters" as Bitstamp specifies in the docs is actually supposed to be sent with content type "application/x-www-form-urlencoded" instead of "application/json".
我也会使用 HttpClient 来执行帖子,因为它有一个更简单的设置来执行 Http 请求
I would also use HttpClient to perform the post as that has a much more simple setup to perform Http requests
using (var client = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("key", apikey),
new KeyValuePair<string, string>("signature", signature),
new KeyValuePair<string, string>("nonce", nonce)
});
var result = await client.PostAsync("https://www.bitstamp.net/api/balance/", content);
string resultContent = await result.Content.ReadAsStringAsync();
}
这篇关于带参数的 Bitstamp API POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!