我在使用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());
当发出完成的(QNetworkReply *)信号时,我发现服务器回复根本不包含任何数据,它只是空的!有谁知道这个问题的解决方案?
UPD 我检查了错误的回复并获得了以下内容:“创建ssl上下文时出错”。所以我在我的项目中添加了SSL库,但是出现了下一个错误 - “未知错误”。有什么想法吗?
答案 0 :(得分: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错误。