我在调用 MTGox HTTP api v2 时遇到问题。我写了一个 sendrequest 函数来处理我所有的请求。它适用于 MONEY/INFO 或 MONEY/ORDERS,但是当我尝试 MONEY/ORDER/QUOTE 或 MONEY/ORDER/ADD 方法时,我收到 500 个内部服务器错误。
似乎当 post_data 包含除了 nonce 之外的任何内容时,它会出错。我该怎么做才能解决这个问题?
发送请求函数:
private string sendRequest(string action, NameValueCollection query)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("nonce", DateTime.Now.Ticks.ToString());
nvc.Add(query);
String post_data = "";
for (int i = 0; i < nvc.Count; i++)
{
post_data += "&";
post_data += nvc.Keys[i];
post_data += "=";
post_data += nvc[i];
}
post_data = post_data.Substring(1);
action = "BTCEUR/money/" + action;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sBasePath + action);
action += "\0"+post_data;
req.Method = "POST";
HMACSHA512 hmac = new HMACSHA512(GetBytes(action));
hmac.Key = Convert.FromBase64String(secret);
String sign = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(action)), Base64FormattingOptions.None);
req.Headers.Add("Rest-Key", apikey);
req.Headers.Add("Rest-Sign", sign);
req.UserAgent = "Mozilla/4.0 (compatible; MtGoxTradeCLI)";
req.ContentType = "application/x-www-form-urlencoded";
StreamWriter reqStream = new StreamWriter(req.GetRequestStream());
reqStream.Write(post_data);
reqStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader respStream = new StreamReader(resp.GetResponseStream());
String response = respStream.ReadToEnd();
respStream.Close();
return response;
}