Adding and Removing Headers
The
RWHttpClient classes automatically add
Host and
Content-Length headers when required by the HTTP protocol, but you can also add headers to the request or you can replace the defaults.
RWHttpRequest takes an optional
RWHttpHeaderList parameter that adds headers to the HTTP request, as shown in
Example 16 .
Example 16 – Adding headers
RWHttpDateHeader date("Sun, 06 Nov 1994 08:49:37 GMT"); // 1
RWHttpHeaderList headerlist;
headerlist.addHeader(date); // 2
RWHttpRequest request(RWHttpRequest::Get, "/", headerlist); // 3
When submitted to an HTTP server through an
RWHttpClient connected to
www.perforce.com, the request sends the following data:
GET / HTTP/1.1
Date: Sun, 06 Nov 1994 08:49:37 GMT
Host: www.perforce.com
The HTTP package also includes:
Helper classes for formatting and parsing specific HTTP headers
A generic header class,
RWHttpGenericHeader, for preparing any label-value pair for inclusion as a header in a HTTP request
For more information about these helper classes, see the
SourcePro API Reference Guide.
RWHttpAgent also enables you to specify additional headers. It has two member functions,
addCustomHeader() and
removeCustomHeader(), that you can use to add headers to outgoing HTTP requests:
Example 17 – Using RWHttpAgent to add headers
RWHttpFromHeader from("user@perforce.com"); // 1
agent.addCustomHeader(from); // 2
Any requests using the agent in
Example 17 would send an additional header line with each request identifying who the request is from.
You can remove headers using the RWHttpAgent::removeCustomHeader() member function.
NOTE: Once a header is associated with an RWHttpAgent object, all requests using that object send that header. If you add a custom header that should only be sent with a particular request, you need to remove it from the agent before issuing more requests.