STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | Public Attributes | List of all members
stt::network::HttpClient Class Reference

Http/Https client operation class. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::HttpClient:
stt::network::TcpClient

Public Member Functions

 HttpClient (const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
 Constructor of HttpClient class. More...
 
bool getRequest (const std::string &url, const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=-1)
 Send a GET request to the server. More...
 
bool postRequest (const std::string &url, const std::string &body="", const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=-1)
 Send a POST request to the server. More...
 
bool getRequestFromFD (const int &fd, SSL *ssl, const std::string &url, const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=2)
 Send a GET request to the server from a tcp socket. More...
 
bool postRequestFromFD (const int &fd, SSL *ssl, const std::string &url, const std::string &body="", const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=2)
 Send a POST request to the server. More...
 
bool isReturn ()
 Get the server response status. More...
 

Public Attributes

std::string header = ""
 Server response header. More...
 
std::string body = ""
 Server response body. More...
 

Detailed Description

Http/Https client operation class.

Note
  • The request header will automatically include Connection: keep-alive
  • If you need to reset the TLS/Https encryption certificate, you currently need to destroy the object and reconstruct it
  • If no socket function is passed, the underlying TCP is blocking by default

Constructor & Destructor Documentation

stt::network::HttpClient::HttpClient ( const bool &  TLS = false,
const char *  ca = "",
const char *  cert = "",
const char *  key = "",
const char *  passwd = "" 
)
inline

Constructor of HttpClient class.

Parameters
TLStrue: Enable Https encryption, false: Do not enable Https encryption (default is false)
caPath to CA root certificate (must be filled if TLS encryption is enabled, default is empty)
certPath to client certificate (optional, default is empty)
keyPath to client private key (optional, default is empty)
passwdPassword for private key decryption (optional, default is empty)
Note
  • ca is used to verify whether the server's certificate is trustworthy
  • If Https encryption is enabled, ca is required, others are optional
  • If the server requires client authentication (two-way TLS/SSL), you need to provide a valid client certificate.

Member Function Documentation

bool stt::network::HttpClient::getRequest ( const std::string &  url,
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = -1 
)

Send a GET request to the server.

Note
To get the result, please call the isReturn function; if there is a result, the response header and body are stored in the global variables header and body.
If TLS is used, the https protocol will be automatically adopted, otherwise the http protocol will be used
is blocking by default
Parameters
urlComplete http/https url (note that the port and path need to be specified explicitly) e.g., https://google.com should be written as https://google.com:443/ (complete with :443 and /)
headerHttp request header; if not generated by createHeader, remember to add
at the end.
header1Additional item for the HTTP request header; if needed, must fill in a valid item; no need to add
at the end (cannot use createHeader). (keepalive item is filled in by default)
secblocking timeout (s) Blocking beyond this time will no longer block The default is -1, i.e. infinite waiting
Returns
true: Request sent successfully, false: Request sending failed Note: This does not mean whether a response is received, only that the sending was successful
Warning
Requires a complete http/https protocol url (note that the port and path need to be specified explicitly even if the path is not needed, fill in /) e.g., https://google.com should be written as https://google.com:443/
bool stt::network::HttpClient::getRequestFromFD ( const int &  fd,
SSL *  ssl,
const std::string &  url,
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = 2 
)

Send a GET request to the server from a tcp socket.

Note
To get the result, please call the isReturn function; if there is a result, the response header and body are stored in the global variables header and body.
If ssl is not nullptr, the https protocol will be automatically adopted, otherwise the http protocol will be used
When calling, it will be blocked, change the status of the original FD, pay attention to backup and restore
Parameters
fdtcp socket
sslTLS encrypted socket
urlComplete http/https url (note that the port and path need to be specified explicitly) e.g., https://google.com should be written as https://google.com:443/ (complete with :443 and /)
headerHttp request header; if not generated by createHeader, remember to add
at the end.
header1Additional item for the HTTP request header; if needed, must fill in a valid item; no need to add
at the end (cannot use createHeader). (keepalive item is filled in by default)
secblocking timeout (s) Blocking beyond this time will no longer block The default is 2s
Returns
true: Request sent successfully, false: Request sending failed Note: This does not mean whether a response is received, only that the sending was successful
Warning
Requires a complete http/https protocol url (note that the port and path need to be specified explicitly even if the path is not needed, fill in /) e.g., https://google.com should be written as https://google.com:443/
bool stt::network::HttpClient::isReturn ( )
inline

Get the server response status.

Returns
true: Server response successful, false: Server response failed
bool stt::network::HttpClient::postRequest ( const std::string &  url,
const std::string &  body = "",
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = -1 
)

Send a POST request to the server.

Note
To get the result, please call the isReturn function; if there is a result, the response header and body are stored in the global variables header and body.
If TLS is used, the https protocol will be automatically adopted, otherwise the http protocol will be used
is blocking by default
Parameters
urlComplete http/https url (note that the port and path need to be specified explicitly) e.g., https://google.com should be written as https://google.com:443/ (complete with :443 and /)
bodyHttp request body
headerHttp request header; if not generated by createHeader, remember to add
at the end.
header1Additional item for the HTTP request header; if needed, must fill in a valid item; no need to add
at the end (cannot use createHeader). (keepalive item is filled in by default)
secblocking timeout (s) Blocking beyond this time will no longer block The default is -1, i.e. infinite waiting
Returns
true: Request sent successfully, false: Request sending failed Note: This does not mean whether a response is received, only that the sending was successful
Warning
Requires a complete http/https protocol url (note that the port and path need to be specified explicitly even if the path is not needed, fill in /) e.g., https://google.com should be written as https://google.com:443/
bool stt::network::HttpClient::postRequestFromFD ( const int &  fd,
SSL *  ssl,
const std::string &  url,
const std::string &  body = "",
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = 2 
)

Send a POST request to the server.

Note
To get the result, please call the isReturn function; if there is a result, the response header and body are stored in the global variables header and body.
If ssl is not nullptr, the https protocol will be automatically adopted, otherwise the http protocol will be used
When calling, it will be blocked, change the status of the original FD, pay attention to backup and restore
Parameters
fdtcp socket
sslTLS encrypted socket
urlComplete http/https url (note that the port and path need to be specified explicitly) e.g., https://google.com should be written as https://google.com:443/ (complete with :443 and /)
bodyhttp request body
headerHttp request header; if not generated by createHeader, remember to add
at the end.
header1Additional item for the HTTP request header; if needed, must fill in a valid item; no need to add
at the end (cannot use createHeader). (keepalive item is filled in by default)
secblocking timeout (s) Blocking beyond this time will no longer block The default is 2s
Returns
true: Request sent successfully, false: Request sending failed Note: This does not mean whether a response is received, only that the sending was successful
Warning
Requires a complete http/https protocol url (note that the port and path need to be specified explicitly even if the path is not needed, fill in /) e.g., https://google.com should be written as https://google.com:443/

Member Data Documentation

std::string stt::network::HttpClient::body = ""

Server response body.

std::string stt::network::HttpClient::header = ""

Server response header.


The documentation for this class was generated from the following file: