我需要使用POST
方法将文件上传到安全主机。这是我尝试的代码(文件可能包含文字内容,也可能是图片。如果是图片,我看here,我可以将ContentTypeHeader
设置为QVariant("image/jpeg")
所以我稍后会尝试。现在我试图上传一个纯文本文件):
QUrl url(urlString); //typical secure host url - myhost.com/logs?key=<filename>&dsn=<some_dsn>&id=<id>&format=<format>
QNetworkRequest request(url);
QSslConfiguration config = request.sslConfiguration();
config.setPeerVerifyMode(QSslSocket::VerifyNone);
config.setProtocol(QSsl::TlsV1_0);
request.setSslConfiguration(config);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
QFile *file = new QFile(QString::fromStdString(fPathToFile));
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(imagePart);
QNetworkAccessManager manager;
QNetworkReply *reply = manager.post(request, multiPart);
connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(onError(QNetworkReply::NetworkError)), Qt::QueuedConnection);
multiPart->setParent(reply);
我在附加的插槽中有一个简单的qDebug
,代码永远不会出现(当然文件也没有正确上传)。我无法理解为什么,它应该是成功还是失败,所以它至少应该来到其中一个插槽。我怀疑我必须正确设置证书。但我无法弄清楚如何。我去了here发布的解决方案,它要求输入密码,这里不需要密码,所以我不知道在那里提供什么。我运行了这个命令:
openssl s_client -showcerts -connect <myhost>:443 </dev/null
这是我得到的输出:
CONNECTED(00000003)
depth=1 /C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
0 s:/C=US/ST=<state>/L=<city>/O=<company>, Inc./CN=<host>
i:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
-----BEGIN CERTIFICATE-----
<random text>
-----END CERTIFICATE-----
1 s:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
-----BEGIN CERTIFICATE-----
<more text>
-----END CERTIFICATE-----
---
Server certificate
subject=/C=US/ST=<state>/L=<city>/O=<company>, Inc./CN=<host>
issuer=/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
---
No client certificate CA names sent
---
SSL handshake has read <some number> bytes and written <some number> bytes
---
New, TLSv1/SSLv3, Cipher is <some cipher>
Server public key is 2048 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : <some cipher>
Session-ID: <some id>
Session-ID-ctx:
Master-Key: <some key>
Key-Arg : None
Start Time: 1452063522
Timeout : 300 (sec)
Verify return code: 0 (ok)
---
DONE
我无法理解如何从此输出中设置证书。如何将文件正确上传到我的主机?
P.S我还有与我的主机相对应的.pem
文件,该文件是在浏览器中通过点击htts://myhost.com
然后单击挂锁图标并从显示的弹出窗口下载文件获得的。这与上面获得的不同。