当我将cookie jar设置为我的网络管理器时,我得到SIGSEGV
,我应该如何将cookiejar设置为我的网络管理员?
我在Windows 10上使用QT5.8与Mingw
PS:我尝试了很多例子,但结果相同。
班级档案:
#include "httpcontroller.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QHttpMultiPart>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QHttpPart>
#include <QFile>
#include <QEventLoop>
HttpController::HttpController(QObject *parent) : QObject(parent)
{
cookies = new QNetworkCookieJar();
manager = new QNetworkAccessManager(parent);
manager->setCookieJar(cookies);
}
QString HttpController::Get(const QString &url)
{
QNetworkRequest request ;
request.setUrl(QUrl(url));
QNetworkReply * reply = manager->get(request);
QEventLoop loop ;
connect(reply,SIGNAL(finished()),&loop,SLOT(quit()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),&loop,SLOT(quit()));
loop.exec();
if(reply->error()){
return QString("Error");
}else{
QString response = QString(reply->readAll()) ;
reply->deleteLater();
return response ;
}
}
QString HttpController::Post(const QString &url, QHttpMultiPart *multipart)
{
QNetworkRequest request ;
request.setUrl(QUrl(url));
QNetworkReply * reply = manager->post(request,multipart);
QEventLoop loop ;
connect(reply,SIGNAL(finished()),&loop,SLOT(quit()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),&loop,SLOT(quit()));
loop.exec();
if(reply->error()){
return QString("Error");
}else{
QString response = QString(reply->readAll()) ;
reply->deleteLater();
return response ;
}
}
标题文件:
#ifndef HTTPCONTROLLER_H
#define HTTPCONTROLLER_H
#include <QObject>
#include <QHttpMultiPart>
#include <QNetworkCookieJar>
#include <QNetworkAccessManager>
class HttpController : public QObject
{
Q_OBJECT
public:
explicit HttpController(QObject *parent = 0);
QString Get(const QString& url);
QString Post(const QString& url , QHttpMultiPart *multipart);
signals:
public slots:
private:
QNetworkCookieJar *cookies;
QNetworkAccessManager *manager ;
};
#endif // HTTPCONTROLLER_H