【问题标题】:Bad HTTP request with QT but good request with CURL带有 QT 的 HTTP 请求错误,但带有 CURL 的请求良好
【发布时间】:2021-04-15 13:44:04
【问题描述】:

如果我从命令行使用 CURL 发出 HTTP 请求(来自 Binance docs 的示例)

curl -H "X-MBX-APIKEY: vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A" -X POST 'https://api.binance.com/api/v3/order/test' -d 'symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559&signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71'

我明白了:

{"code":-2015,"msg":"Invalid API-key, IP, or permissions for action."}

如果我这样做了

QNetworkRequest request;

request.setRawHeader(QByteArray("X-MBX-APIKEY"), QString("vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A").toUtf8());
request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/x-www-form-urlencoded"));

request.setUrl(QUrl("https://api.binance.com/api/v3/order/test"));

QByteArray postData = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559&signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71";
QNetworkReply* reply = m_nm.post(request, postData);

QObject::connect(reply, &QNetworkReply::finished, [this, reply, url, func]()
{
    if (reply->error())
    {
        qDebug() << "Error: " << reply->error() << ", Message: " << reply->errorString();
});
    }
    else
    {
        const QString answer = reply->readAll();

        func(answer);
    }

    //Ensure it is deleted after this handler is called, but not before.
    //reply->deleteLater();
    delete reply;
});

我收到一条错误回复,消息为Error: 204, Message: Host requires authentication

使用其他 API 密钥,我得到 Bad requestQNetworkReply::ProtocolInvalidOperationError

有什么区别?

EDIT1:

请求成功

request.setUrl(QUrl("https://httpbin.org/post"));

和相同的帖子数据,例如。

EDIT2:

QT adds

"Accept-Encoding": "gzip, deflate", 
"Accept-Language": "en-GB,*"

但不是他们。

【问题讨论】:

    标签: qt binance qtnetwork


    【解决方案1】:

    HTTP 401 错误代码表明请求没有被执行,因为它缺少对所请求资源的有效身份验证凭据,这会导致 Qt 创建错误 QNetworkReply::AuthenticationRequiredError,除此之外,还有有关原因的信息响应正文中的错误。因此,即使出现错误,也必须检查数据。

    MWE:

    #include <QtNetwork>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QNetworkAccessManager m_nm;
        QNetworkRequest request;
        request.setRawHeader(QByteArray("X-MBX-APIKEY"), QString("vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A").toUtf8());
        request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/x-www-form-urlencoded"));
        request.setUrl(QUrl("https://api.binance.com/api/v3/order/test"));
        QByteArray postData = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559&signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71";
        QNetworkReply* reply = m_nm.post(request, postData);
    
        QObject::connect(reply, &QNetworkReply::finished, [reply]()
        {
            const QString body = reply->readAll();
            if (reply->error() == QNetworkReply::AuthenticationRequiredError){
                qDebug() << "Error: " << reply->error() <<
                            ", Message: " << reply->errorString() <<
                            ", Code: " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() <<
                            ", Description: " << body;
            }
            else if (reply->error() != QNetworkReply::NoError){
                qDebug() << body;
            }
            reply->deleteLater();
            QTimer::singleShot(1000, &QCoreApplication::quit);
        });
    
        return a.exec();
    }
    

    输出:

    Error:  QNetworkReply::AuthenticationRequiredError , Message:  "Host requires authentication" , Code:  401 , Description:  "{\"code\":-2015,\"msg\":\"Invalid API-key, IP, or permissions for action.\"}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2016-11-10
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      相关资源
      最近更新 更多