0

我在使用 QNetworkAccessManager 获取 Microsoft 翻译器的应用程序令牌时遇到问题。这是我的代码:

QUrl serverUrl("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");
QNetworkRequest request(serverUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrl postData;
postData.addQueryItem("grant_type", "client_credentials");
postData.addQueryItem("client_id", ClientID);
postData.addQueryItem("client_secret", ClientSecret);
postData.addQueryItem("scope", "http://api.microsofttranslator.com");
networkAccessManager->post(request, postData.encodedQuery());

当发出 finished(QNetworkReply*) 信号时,我发现服务器回复根本不包含任何数据,它只是空的!有谁知道这个问题的解决方案?
UPD我检查了错误回复并得到以下信息:“创建 ssl 上下文时出错”。所以我将 SSL 库添加到我的项目中,但出现下一个错误 - "Unknown error"。有任何想法吗?

4

1 回答 1

0

我已经使用从 QNetworkAccessManager 子类化的这个类来忽略 SSL 错误:

sslqnetworkaccessmanager.h

#ifndef SSLQNETWORKACCESSMANAGER_H
#define SSLQNETWORKACCESSMANAGER_H
#include <QtNetwork>
#include <QtCore>

class SslQNetworkAccessManager : public QNetworkAccessManager
{
    Q_OBJECT
public:
    explicit SslQNetworkAccessManager(QObject *parent = 0);


protected:
    QNetworkReply* createRequest(Operation op, const QNetworkRequest & request, QIODevice * outgoingData = 0);
};

#endif // SSLQNETWORKACCESSMANAGER_H

sslqnetworkaccessmanager.cpp

#include "sslqnetworkaccessmanager.h"

SslQNetworkAccessManager::SslQNetworkAccessManager(QObject *parent)
    : QNetworkAccessManager(parent)
{
}

QNetworkReply* SslQNetworkAccessManager::createRequest(Operation op, const QNetworkRequest& request, QIODevice* outgoingData)
{
    QNetworkRequest req(request);
    QNetworkReply* reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
    reply->ignoreSslErrors();

    return reply;
}

此类覆盖 createRequest 方法并忽略返回的 QNetworkReply 可能发生的任何 SSL 错误。

于 2013-07-23T15:01:10.803 回答