Qt HTTP POST request (Json format and Form format) to access the server

access server in form format

QByteArray postArray;
postArray.append("grant_type=authorization_code");
postArray.append("&client_id=32u2w95f200D4d27");
postArray.append("&client_secret=37vH35Eg028P2HZz");
postArray.append("&redirect_uri=http://www.163.com");
postArray.append("&code=128a55c1249ebf3ef9d154b72a5386d4");

QNetworkRequest request(QUrl("http://kk.bigk2.com:8080/KOAuthDemeter/accessToken"));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
request.setHeader(QNetworkRequest::ContentLengthHeader,postArray.size());
manager->post(request,postArray);

Access the server in json format

 QUrl url("http://kk.bigk2.com:8080/KOAuthDemeter/User/getKSceneList");
QByteArray array("{\"userid\":\"[email protected]\"}");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
request.setRawHeader("Authorization","Bearer 49152bfddae0c5b5d492d3d9871f8c11");//服务器要求的数据头部
manager->post(request,array);

HTTP access server

To access a server using HTTP, you need to send a packet format that the server can accept to the server. 
Start: The format of an Ethernet packet is: header (Head) and data (Data). The header contains the sender, receiver, and data type. The sender and receiver are automatically encapsulated in some high-level classes. All we have to do is set the data type. By setting the correct data type, and then sending the data out. Simply put: I need to send a packet to the server. First make sure that the type of data the server accepts is "application/x-www-form-urlencoded", so I set the data type of the header to "application/x-www-form-urlencoded", and then assemble the data. As a result, one packet is complete. header + data = a complete packet. Next, a brief introduction to some HTTP concepts

  1. Resources: A web server hosts web resources. Web resources are the source of web content
  2. MIME type: The Web server attaches a MIME (Multipurpose Internet Mail Extension) type to all HTTP object data. The above "application/x-www-form-urlencoded" is a MIME type. Originally designed to solve the problem of moving messages between different email systems. A MIME type is a file marker that represents a main object type and a specific subtype, separated by a slash. For example: "application/x-www-form-urlencoded" we used above
  3. URI: The server resource name is the Uniform Resource Identifier (URI). URIs come in two forms, URL and URN.
  4. URL: Uniform Resource Locator (URL) is the most common form of resource identifier. A URL defies a specific location of a resource on a specific server. Most URLs follow a standard format that consists of three parts: 
    • The first part of the URL is called the scheme and specifies the type of protocol used to access the resource. This part usually uses the HTTP (Hypertext Transfer Protocol) protocol ("http:/"), in addition to the HTTPS (Hypertext Transfer Protocol Secure) protocol.
    • The second part gives the Internet address of the server (eg  https://en.wikipedia.org )
    • The rest is given a resource on the web server (eg: /wiki/Cookie)
  5. URN: The second form of URI is the Uniform Resource Name (URN). Less used, no discussion.
  6. Transaction: An HTTP transaction consists of a request server command and a server response result. This communication is via formatted blocks of HTTP messages.
  7. Status code: Each HTTP response packet returns with a status code. The status code is a three-digit code that informs the client of the result of the request.
  8. Message: An HTTP message consists of a line-by-line simple string. HTTP messages are all plain text, not binary, so people can easily read and write them. The message from the client to the server is called the request message, and the message from the server to the client is the response message. The HTTP message consists of three parts: Detailed HTTP message 
    • request line
    • request header
    • main body
  9. Proxy: A proxy sits between the client and the server and accepts all client HTTP requests and forwards those requests to the server.
  10. For more articles, look up the corresponding keywords in Wikipedia.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518268&siteId=291194637