我尝试将请求发送到btc-e.com(文档https://btc-e.com/api/documentation) 有我的代码,但
QString str;
QDateTime qtime = QDateTime::currentDateTime();
qint64 time = qtime.toMSecsSinceEpoch()/1000;
str.setNum(time);
str = "method=getInfo&nonce=" + str;
QByteArray sign = hmacSha512(secret,str);
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRawHeader("Key", key);
request.setRawHeader("Sign", sign);
post->post(request,str.toUtf8());
由于此请求的结果是错误“400 BAD REQUEST”,我找不到任何错误。请帮我。可能是我发送了一个错误的请求,因为没有描述正确的例子。
答案 0 :(得分:0)
您尚未发布所有代码,因此很难提供帮助。
根据屏幕截图获取http OK - 200
我添加了一些代码并使用Qt 5.2在Mac上构建它:
#include <QCoreApplication>
#include <QNetworkRequest>
#include <QDateTime>
#include <QNetworkAccessManager>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include "myhandler.h"
// Thanks to http://www.askyb.com/cpp/openssl-hmac-hasing-example-in-cpp/
QByteArray hmacSha512(const QString &secret, const QString &str)
{
unsigned char* digest;
digest = HMAC(EVP_sha512(), qPrintable(secret), secret.length(), (unsigned char*)qPrintable(str), str.length(), NULL, NULL);
char mdString[EVP_MAX_MD_SIZE];
for(int i = 0; i < 20; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
printf("HMAC digest: %s\n", mdString);
return QByteArray(mdString);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyHandler *handler = new MyHandler;
// The key to hash
QByteArray key = "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T";
QString secret = "12345678";
QUrl url("https://btc-e.com/tapi");
QNetworkAccessManager *post = new QNetworkAccessManager();
QObject::connect(post, SIGNAL(finished(QNetworkReply*)),
handler, SLOT(replyFinished(QNetworkReply*)));
// ----8<----------- YOUR CODE -----------
QString str;
QDateTime qtime = QDateTime::currentDateTime();
qint64 time = qtime.toMSecsSinceEpoch()/1000;
str.setNum(time);
str = "method=getInfo&nonce=" + str;
QByteArray sign = hmacSha512(secret,str);
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRawHeader("Key", key);
request.setRawHeader("Sign", sign);
post->post(request,str.toUtf8());
// ----8<----------- YOUR CODE -----------
return a.exec();
}
只是一个小型处理程序类
#ifndef MYHANDLER_H
#define MYHANDLER_H
#include <QObject>
class QNetworkReply;
class MyHandler : public QObject
{
Q_OBJECT
public:
explicit MyHandler(QObject *parent = 0);
signals:
public slots:
void replyFinished(QNetworkReply *reply);
};
#endif // MYHANDLER_H
和实施:
#include "myhandler.h"
#include <QNetworkReply>
#include <QDebug>
#include <QTextStream>
MyHandler::MyHandler(QObject *parent) :
QObject(parent)
{
}
void MyHandler::replyFinished(QNetworkReply *reply)
{
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QTextStream ts(reply);
while (!ts.atEnd())
{
qDebug() << ts.readLine();
}
}