Qt如何向服务器发送https请求?

时间:2013-12-07 13:13:33

标签: c++ qt https request

我尝试使用 https url发送get请求。回复为空,但没有错误消息。我下载OpenSSL并将 libeay32.dll ssleay32.dll 文件复制到C:\ Qt \ Qt5.1.1 \ Tools \ QtCreator \ bin文件夹。

代码:

QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest request;
QNetworkReply *reply = NULL;

QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setProtocol(QSsl::TlsV1_2);
request.setSslConfiguration(config);
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ServerHeader, "application/json");

reply = manager->get(request);

qDebug() << reply->readAll();

1 个答案:

答案 0 :(得分:3)

正如弗兰克在他的共通中写道的那样,get函数是异步的,所以当你试图读取答案时,http请求可能还没有完成。

要解决此问题,您需要处理finished信号:

connect(manager, SIGNAL(finished(QNetworkReply*)), this,
                 SLOT(replyFinished(QNetworkReply*)));

并在处理程序中读取结果:

void NetworkHandler::replyFinished(QNetworkReply *reply)
{
  qDebug() << reply->readAll();
}