DataReelDataReelC++ Toolkit Documentation

HTTPS Client

gxsHTTPClient sends HTTPS requests and follows redirects. Plain http:// is refused.

Contents

Overview
Functions

Overview

Each request opens a new TLS connection, checks the server certificate, sends the request with Connection: close, and reads the whole response into a gxsHTTPMessage. Up to five redirects are followed (SetMaxRedirects()), only to https:// URLs; credentials are not sent to a different host after a redirect. User names and passwords in the URL, or set with SetBasicAuth(), are sent as basic authentication.

gxsHTTPClient client;
gxsHTTPMessage resp;
if(client.Get("https://datareel.com/", resp) == 0)
  std::cout << resp.status << " " << resp.body.size() << " bytes\n";
else
  std::cerr << client.Error() << "\n";

Functions

int gxsHTTPClient::Get(const std::string &url, gxsHTTPMessage &response)

GET request. Returns 0 if a response was received (check response.status).

int gxsHTTPClient::Post(const std::string &url, const std::string &body, const std::string &content_type, gxsHTTPMessage &response)

POST request.

int gxsHTTPClient::Request(const std::string &method, const std::string &url, const std::string &body, const std::string &content_type, gxsHTTPMessage &response)

Any request method.

int gxsHTTPClient::Download(const std::string &url, const std::string &fname, gxsHTTPMessage &response)

GET url and write the body to fname. Returns 0 if the status was 2xx and the file was written.

void gxsHTTPClient::SetTimeout(int seconds)

Connect and read timeout (60 seconds by default).

void gxsHTTPClient::SetCAFile(const std::string &fname) / SetVerifyPeer(bool on)

Certificate checking, as for gxsTLSConnection.

void gxsHTTPClient::SetUserAgent(const std::string &s)

User-Agent header (DataReel/7 by default).

void gxsHTTPClient::SetBasicAuth(const std::string &user, const std::string &passwd)

Basic authentication for every request.

void gxsHTTPClient::AddHeader(const std::string &name, const std::string &value)

Extra header sent with every request.

void gxsHTTPClient::SetMaxRedirects(int n) / SetMaxBodySize(std::size_t n)

Limits.

const std::string &gxsHTTPClient::Error() const

Describes the last failure. FinalURL() gives the URL after redirects.