A HTTP client. More...
#include <Http.hpp>
Classes | |
class | Request |
Define a HTTP request. More... | |
class | Response |
Define a HTTP response. More... | |
Public Member Functions | |
Http () | |
Default constructor. | |
Http (const std::string &host, unsigned short port=0) | |
Construct the HTTP client with the target host. | |
void | SetHost (const std::string &host, unsigned short port=0) |
Set the target host. | |
Response | SendRequest (const Request &request, Uint32 timeout=0) |
Send a HTTP request and return the server's response. |
A HTTP client.
sf::Http is a very simple HTTP client that allows you to communicate with a web server.
You can retrieve web pages, send data to an interactive resource, download a remote file, etc.
The HTTP client is split into 3 classes:
sf::Http::Request builds the request that will be sent to the server. A request is made of:
sf::Http::Response parse the response from the web server and provides getters to read them. The response contains:
sf::Http provides a simple function, SendRequest, to send a sf::Http::Request and return the corresponding sf::Http::Response from the server.
Usage example:
// Create a new HTTP client sf::Http http; // We'll work on http://www.sfml-dev.org http.SetHost("http://www.sfml-dev.org"); // Prepare a request to get the 'features.php' page sf::Http::Request request("features.php"); // Send the request sf::Http::Response response = http.SendRequest(request); // Check the status code and display the result sf::Http::Response::Status status = response.GetStatus(); if (status == sf::Http::Response::Ok) { std::cout << response.GetBody() << std::endl; } else { std::cout << "Error " << status << std::endl; }
Definition at line 44 of file Http.hpp.
sf::Http::Http | ( | ) |
Default constructor.
sf::Http::Http | ( | const std::string & | host, | |
unsigned short | port = 0 | |||
) |
Construct the HTTP client with the target host.
This is equivalent to calling SetHost(host, port). The port has a default value of 0, which means that the HTTP client will use the right port according to the protocol used (80 for HTTP, 443 for HTTPS). You should leave it like this unless you really need a port other than the standard one, or use an unknown protocol.
host | Web server to connect to | |
port | Port to use for connection |
Send a HTTP request and return the server's response.
You must have a valid host before sending a request (see SetHost). Any missing mandatory header field in the request will be added with an appropriate value. Warning: this function waits for the server's response and may not return instantly; use a thread if you don't want to block your application, or use a timeout to limit the time to wait. A value of 0 means that the client will use the system defaut timeout (which is usually pretty long).
request | Request to send | |
timeout | Maximum time to wait, in milliseconds |
void sf::Http::SetHost | ( | const std::string & | host, | |
unsigned short | port = 0 | |||
) |
Set the target host.
This function just stores the host address and port, it doesn't actually connect to it until you send a request. The port has a default value of 0, which means that the HTTP client will use the right port according to the protocol used (80 for HTTP, 443 for HTTPS). You should leave it like this unless you really need a port other than the standard one, or use an unknown protocol.
host | Web server to connect to | |
port | Port to use for connection |