Qt provides qhttp to implement communication between the QT code and the HTTP server, and provides get and post methods for sending requests. Students who have done this website should know that the former is used to transmit a small amount of data, what if the latter is used to transmit a large amount of data? It should be more than 1024 characters ~ For example, 100 + kb or 1 MB + is relatively large!
However, as detailed description of qhttp, The get and post functions provided by qhttp are not allowed to pass parameters. They can only access the page and cannot pass parameters to the page. It can be said that the function is very simple.
To pass parameters to the server, I know two methods:
1. Use qnetworkaccessmanager instead of qhttp to access the HTTP server. qnetworkaccessmanager is a high-level encapsulation of qhttp, which is very easy to use. ReferInstances on the official QT website.
2. insist on using qhttp, but use the request function to send an HTTP request, instead of the get or post function. I will mainly talk about this method here ^_^
The request function requires a qhttprequestheader, and some require a qbytearray parameter to store the parameters to be sent to the server, which is not the main one. What's important is how to set this qhttprequestheader!
Directly run the Code:
1. Use the get method to pass Parameters to the server through the request Function
Qhttprequestheader header ("get", "/mywebapp/myjsp. jsp? Code = 1 & account = 10 & Pwd = 12 "); // the parameters to be passed are directly written in the path header. setvalue ("host", "127.0.0.1"); // you must set the Host IP address or name qhttp HTTP; http. sethost ("127.0.0.1", 8080); // set the Host IP address and port number int http_request_id = http for qhttp. request (header); // send the HTTP request. close (); // close the request. We recommend that you close the request because after close, the output buffer will be flushed ~
2. Use the POST method to pass parameters to the server through the request Function
Qhttprequestheader header ("Post", "/mywebapp/myjsp. jsp"); // only sets the path of the page. parameters to be sent cannot be written here! Header. setvalue ("host", "127.0.0.1"); header. setcontenttype ("application/X-WWW-form-urlencoded"); // you must set this parameter! Qhttp HTTP; http. sethost ("127.0.0.1", 8080); // set the Host IP address and port number int http_request_id = http for qhttp. request (header, qstring ("code = 1 & account = 10 & Pwd = 12 "). toutf8 (); // send the request. Note that you need to use the request function overload version that requires the qbytearray parameter to send the HTTP parameter to the server. close (); // close the request. We recommend that you close the request because after close, the output buffer will be flushed ~
Well, we will find that the GET parameters are doped in the path of the page, while the post parameters are added separately when the qhttp: Request () function is executed, note that if you use post to pass parameters, you must set contenttype to "application/X-WWW-form-urlencoded". Otherwise, the server cannot find these parameters!
The text is complete.
I learned it after one afternoon and one night, hoping to help you!
The following are some references:
1. http request (difference between get and post) and response (very thorough introduction to get and post)
2. QT sends an HTTP request (I personally think this blog is very good !)
3. Experience in qhttp programming (as an extension)