Qt sends HTTP requests using HTTPS (using the OpenSSL library)

Source: Internet
Author: User

First, brief

The usual link used in sending HTTP requests using QT is a/ http prefix, while some servers support https:// prefixes, while QT itself is HTTPS -enabled, but HTTPS access SSL authentication is required, and QT does not support SSL authentication By default, so using https in QT requires SSL authentication. The implementation is very easy and can be done in a few simple steps.

Ii. the difference between HTTPS and HTTP 1, Introduction

Hypertext Transfer Protocol HTTP is used to pass information between a Web browser and a Web server. The HTTP protocol sends content in plaintext, does not provide data encryption in any way, and if an attacker intercepts a transmission message between a Web browser and a Web server, it can read the information directly, so the HTTP protocol is not suitable for transmitting sensitive information such as credit card numbers, passwords, etc.

To address this flaw in the HTTP protocol, you need to use a different protocol: Secure Sockets Layer Hypertext Transfer Protocol HTTPS. For the security of data transmission, HTTPS joins the SSL protocol on the basis of HTTP, SSL relies on the certificate to verify the identity of the server, and encrypts the communication between the browser and the server.

2. Difference

The HTTPS protocol requires a certificate to be applied to the CA, and the general free certificate is very small and requires a fee.

HTTP is a Hypertext Transfer Protocol, the information is plaintext transmission, HTTPS is a secure SSL encryption transport protocol.

HTTP and HTTPS use a completely different connection, the same port, the former is 80, the latter is 443.

The HTTP connection is simple and stateless; The HTTPS protocol is a network protocol built by the SSL+HTTP protocol for encrypted transmission and authentication, which is more secure than the HTTP protocol.

Third, how to carry out SSL authentication 1, first, because the need for OpenSSL support, the use of SSL authentication, so the corresponding library files are requiredLibeay32.dllAndSsleay32.dll。 Need to downloadLibeay32.dllAndSsleay32.dllTwo library files, copy the two library files to the program Generation directory (that is, generate the EXE's sibling directory) or copy to the library file directory of the Qtnetwork module. 2. Add the following code before sending the HTTP request
void onsendhttpsrequest (QString url) {Qnetworkaccessmanager* Pmanager=New Qnetworkaccessmanager (this); Qnetworkrequest request;Prepare for the HTTPS request before sending it; Qsslconfiguration config; Qsslconfiguration conf= Request. Sslconfiguration (); Conf. Setpeerverifymode (Qsslsocket:: Verifynone); Conf. Setprotocol (Qssl:: Tlsv1sslv3); Request. setsslconfiguration (conf); Request. SetUrl (Qurl (URL));"Https://www.temp.com" qnetworkreply*preply= Pmanager->post (Request, bytepost); Connect (preply, SIGNAL (finished ()), this, SLOT (onfinished ()), connect (preply, SIGNAL (Error (qnetworkreply:: Networkerror)), this, SLOT (OnError (qnetworkreply:: Networkerror)));}Request completed;void onfinished () {qnetworkreply *preplay = qobject_cast<qnetworkreply*> (Sender ()); //to save the accepted data; Qbytearray replycontent = preplay->readall ();} //request failed; void onError (Qnetworkreply::networkerror ErrorCode) { Qnetworkreply *preplay = qobject_cast <qnetworkreply*> (Sender ()); //output error code and error message; Qdebug () << ErrorCode; Qdebug () << preplay->errorstring ();       
About two library file downloads

1. Download Win32 OpenSSL in http://slproweb.com/products/Win32OpenSSL.html to install, copy the library below the bin directory and put it in the generator exe sibling directory.

2, Libeay32.dll, Ssleay32.dll dynamic link library directly in my resources to download.

http://blog.csdn.net/goforwardtostep/article/details/53588961

Qt sends HTTP requests using HTTPS (using the OpenSSL library)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.