I need to upload a file to a secure host using POST
method. This is the code I tried (the file may have either text content or it may be an image. In case of image, as I see here, I can set the ContentTypeHeader
to QVariant("image/jpeg")
. So I will try that later. Now I am trying to upload a plain text file):
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);
I have a simple qDebug
in the attached slots, the code never ever comes there (and of course the file does not get uploaded correctly). I can't understand why, it should be either a success, or a failure, so it should come to one of the slots at least. I suspect that I have to set the certificates correctly. But I am not able to find out how. I went though the solutions posted here, it asks for password, and none is needed here, so I don't know what to provide there. I ran this command:
openssl s_client -showcerts -connect <myhost>:443 </dev/null
This is the output I get:
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
I can't understand how to set the certs from this output. How do I upload the file correctly to my host?
P.S I also have the .pem
file corresponding to my host, that was obtained in the browser by hitting htts://myhost.com
and then clicking the padlock icon and downloading the file from the popup shown. That comes out to be different that the one obtained above.