2

我需要使用方法将文件上传到安全主机POST。这是我尝试过的代码(文件可能有文本内容,也可能是图像。如果是图像,如我在此处看到的,我可以将其设置ContentTypeHeaderQVariant("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在附加的插槽中有一个简单的,代码永远不会出现(当然文件没有正确上传)。我不明白为什么,它应该是成功的,或者是失败的,所以它至少应该出现在其中一个位置上。我怀疑我必须正确设置证书。但我不知道怎么做。我浏览了这里发布的解决方案,它要求输入密码,这里不需要密码,所以我不知道在那里提供什么。我运行了这个命令:

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

我不明白如何从此输出设置证书。如何将文件正确上传到我的主机?

PS我也有.pem与我的主机相对应的文件,该文件是通过点击htts://myhost.com然后单击挂锁图标并从显示的弹出窗口中下载文件在浏览器中获得的。结果与上面得到的不同。

4

0 回答 0