Public Member Functions | List of all members
BHttpRequest Class Reference

Handles a request over HTTP or HTTPS. More...

Inherits BNetworkRequest.

Public Member Functions

void AdoptHeaders (BHttpHeaders *const headers)
 Set the HTTP headers. More...
 
void AdoptInputData (BDataIO *const data, const ssize_t size=-1)
 Set the request body. More...
 
void AdoptPostFields (BHttpForm *const fields)
 Set the fields for form POST data. More...
 
void SetAutoReferrer (bool enable)
 Automatically set the referrer when the request is done. More...
 
void SetFollowLocation (bool follow)
 Enable or disable following HTTP redirects. More...
 
void SetHeaders (const BHttpHeaders &headers)
 Set the HTTP headers. More...
 
void SetMaxRedirections (int8 maxRedirections)
 Set the maximal number of redirections to follow before giving up. More...
 
void SetMethod (const char *const method)
 Set the HTTP method. More...
 
void SetPassword (const BString &password)
 Set the user password for HTTP authentication.
 
void SetPostFields (const BHttpForm &fields)
 Set the fields for form POST data. More...
 
void SetReferrer (const BString &referrer)
 Set the referrer. More...
 
void SetUserAgent (const BString &agent)
 Set the user agent. More...
 
void SetUserName (const BString &name)
 Set the user name for HTTP authentication.
 

Detailed Description

Handles a request over HTTP or HTTPS.

Instances of ths class will be created by the BUrlProtocolRoster for BUrl with the "http" or "https" protocol. The HTTP protocol is implemented as specified in RFC2616. The request headers and body can be customized, then sent to the server. The reply is then parsed and made available to the application.

This class only implements the client-side part of HTTP, it can't be used to build an HTTP server.

Member Function Documentation

◆ AdoptHeaders()

void BHttpRequest::AdoptHeaders ( BHttpHeaders *const  headers)

Set the HTTP headers.

This method replaces the whole set of headers for this request. It takes ownership of the parameter, which must not be used afterwards.

◆ AdoptInputData()

void BHttpRequest::AdoptInputData ( BDataIO *const  data,
const ssize_t  size = -1 
)

Set the request body.

If the size is -1 (the default), the data will be sent using chunked transfers. If the size is known, it will be sent using the Content-Length header and non-chunked mode.

You should set the size whenever possible, as some servers will not handle chunked mode properly in all cases.

This method takes ownership of the data, which must not be used elsewhere.

◆ AdoptPostFields()

void BHttpRequest::AdoptPostFields ( BHttpForm *const  fields)

Set the fields for form POST data.

Replaces the content of the request with the given POST fields.

This method takes ownership of the given form, which must not be used elsewhere afterwards.

◆ SetAutoReferrer()

void BHttpRequest::SetAutoReferrer ( bool  enable)

Automatically set the referrer when the request is done.

This allows HttpRequest to manage the referrer automatically. Each request will set the referrer to its own URL so the next request automatically uses that one.

◆ SetFollowLocation()

void BHttpRequest::SetFollowLocation ( bool  follow)

Enable or disable following HTTP redirects.

An HTTP server can redirect a request to another address, either on the same host or elsewhere. When FollowLocation is set (the default), these redirections will be followed until an actual page (or an error) is found. When it is unset, the redirection will not be followed and will be reported to the client.

◆ SetHeaders()

void BHttpRequest::SetHeaders ( const BHttpHeaders headers)

Set the HTTP headers.

This method replaces the whole set of headers for this request with a copy of the given ones.

Parameters
headersthe header template to copy from.

◆ SetMaxRedirections()

void BHttpRequest::SetMaxRedirections ( int8  maxRedirections)

Set the maximal number of redirections to follow before giving up.

This is only useful when SetFollowLocation is enabled. It will abort the request after the given number of redirections. This avoids and helps diagnosing redirection cycles, where two addresses redirect to each other.

The default is to follow at most 8 redirections before giving up.

◆ SetMethod()

void BHttpRequest::SetMethod ( const char *const  method)

Set the HTTP method.

You can use either one of the standard methods (B_HTTP_GET is the default) or a custom one. The standard methods are B_HTTP_GET, B_HTTP_POST, B_HTTP_PUT, B_HTTP_DELETE, B_HTTP_HEAD, B_HTTP_OPTIONS, B_HTTP_TRACE and B_HTTP_CONNECT.

◆ SetPostFields()

void BHttpRequest::SetPostFields ( const BHttpForm fields)

Set the fields for form POST data.

Replaces the content of the request with a copy of the given POST fields.

◆ SetReferrer()

void BHttpRequest::SetReferrer ( const BString referrer)

Set the referrer.

The referrer is a string sent to the server in the "Referrer:" HTTP header field. It helps the server know where the request comes from. When following a link in an HTML page, this is usually set to the URL of that page.

◆ SetUserAgent()

void BHttpRequest::SetUserAgent ( const BString userAgent)

Set the user agent.

The user agent is an identifier for the client sending an HTTP request. Some servers will use this to send different content depending on the software asking for a page.

The default user agent is "Services Kit (Haiku)".