QNetworkAccessManager永远不会发出finished()信号

时间:2017-10-12 12:35:24

标签: c++ qt http ssl http-get

我正在处理一个项目的模块,其中HTTP GET请求用于检索某些XML数据,然后将其转换为另一种格式并发送到子系统。

我到目前为止编写的代码如下:

的CMakeLists.txt:

project(HttpDemo)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPE Debug)

#find_package(Qt5Widgets)
find_package(Qt5Core)
find_package(Qt5Network)

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
qt5_use_modules(${PROJECT_NAME} Core Network) #Gui Widgets

的main.cpp

#include <QtCore>
#include <QtNetwork>

class HttpHandler : public QObject
{
    Q_OBJECT
public:
    HttpHandler(QObject* parent=Q_NULLPTR) : QObject(parent)
    {
        QObject::connect(&nm, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
        qDebug() << QSslSocket::sslLibraryBuildVersionString();
    }
private:
    QNetworkAccessManager nm;
public slots:
    void post(QString urlLink)
    {
        QUrl url(urlLink);
        QNetworkRequest request(url);
        QSslConfiguration sslConf;
        sslConf.setProtocol(QSsl::SslV3);
        request.setSslConfiguration(sslConf);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencded");

        QUrlQuery query;
        query.addQueryItem("client_id", "1234");
        query.addQueryItem("code", "abcd");

        QUrl params;
        params.setQuery(query);

        nm.post(request, params.toEncoded());
    }

    void get(QString urlLink)
    {
        QUrl url(urlLink);
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        nm.get(request);
    }

    void replyFinished(QNetworkReply* reply)
    {
        QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
        if(statusCode.isValid())
        {
            // Print or catch the status code
            QString status = statusCode.toString(); // or status_code.toInt();
            qDebug() << status;
            qDebug() << QString::fromStdString(reply->readAll().toStdString());
        }
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    HttpHandler hh;
    hh.get("SOME_URL");

    return a.exec();
}

#include "main.moc"

使用SOME_URL我尝试了很多链接,所有这些链接都没有任何问题,让我们说Firefox的 Http Requester 插件。我明白了:

  

&#34; OpenSSL 1.0.1j 2014年10月15日&#34;

     

qt.network.ssl:QSslSocket:无法解析SSLv2_client_method

     

qt.network.ssl:QSslSocket:无法解析SSLv2_server_method

根据称为互联网的权威机构,这不应该是一个问题。有一件事是肯定的 - 虽然replyFinished(QNetworkReply*)插槽连接到finished()的{​​{1}}信号,但我的QNetworkAccessManager插槽无法触发。这意味着无论发出信号的原因是什么。将QSslConfiguration更改为其他QSsl::SslProtocol并不会对结果产生影响。

更新(根据评论中的要求):

以下代码使用readyRead()并动态分配回复。结果 - 与上述相同。

#include <QtCore>
#include <QtNetwork>

class HttpHandler : public QObject
{
    Q_OBJECT
public:
    HttpHandler(QObject* parent=Q_NULLPTR) : QObject(parent)
    {
        qDebug() << QSslSocket::sslLibraryBuildVersionString();
        this->manager = new QNetworkAccessManager(this);
        this->reply = Q_NULLPTR;
    }
private:
    QNetworkAccessManager* manager;
    QNetworkReply* reply;
signals:
    void finished();
public slots:
    void post(QString urlLink)
    {
        QUrl url(urlLink);
        QNetworkRequest request(url);
        QSslConfiguration sslConf;
        sslConf.setProtocol(QSsl::SslV2);
        request.setSslConfiguration(sslConf);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        QUrlQuery query;
        query.addQueryItem("client_id", "1234");
        query.addQueryItem("code", "abcd");

        QUrl params;
        params.setQuery(query);

        manager->post(request, params.toEncoded());
    }

    void get(QString urlLink)
    {
        QUrl url(urlLink);
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        this->reply = manager->get(request);
        QObject::connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
    }

    void slotReadyRead()
    {
        qDebug() << "Hello"; // I never land here
    }

    void replyFinished(QNetworkReply* reply)
    {
        QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
        if(statusCode.isValid())
        {
            QString status = statusCode.toString(); // or status_code.toInt();
            qDebug() << status;
            qDebug() << QString::fromStdString(reply->readAll().toStdString());
        }

        emit finished();
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    HttpHandler *hh = new HttpHandler(&a);
    QObject::connect(hh, SIGNAL(finished()), &a, SLOT(quit()));

    hh->get("http://httpbin.org/ip"); // or any other httpbin.org endpoint

    return a.exec();
}

#include "main.moc"

更新2:

我刚发现example in the Qt documentation。下载,编译和运行的东西 - 相同的错误,但它的工作原理。

1 个答案:

答案 0 :(得分:0)

问题已解决(请参阅here)。基本上问题是公司的代理。我的一位同事给了它一个镜头并用HTTPS取代了HTTP(即使链接是HTTP),它突然起作用了。然后它打动了我们 - 公司的代理缓存HTTP(并做其他事情),这会导致巨大的延迟,如果超时容限足够小QNetworkAccessManager将返回套接字超时。

使用QNetworkProxyFactory::setUseSystemConfiguration(true)启用代理的方式不会使您的应用程序依赖于代码中的配置,而是依赖于系统的配置。