Sending Requests through HTTP Proxies

When HTTP requests are sent to an HTTP proxy (such as a firewall) that forwards the request to the target HTTP server, your application must include the following steps:

The client must connect to the proxy HTTP server instead of the actual HTTP server.

The request must contain a Host header that points to the actual server that the request is intended for.

Example 293 shows a simple GET request executed through a proxy HTTP server.

Servers and files shown in the code might not exist and are included as examples only.

Example 293. Sending a request through a proxy HTTP server

RWHttpClient client = RWHttpSocketClient::make();

 

RWCString proxyServer = "proxy.somehost.com"; // 1

RWCString actualServer = "www.perforce.com"; // 2

RWCString path = "/";

 

client.connect(proxyServer); // 3

 

RWHttpHostHeader hostHeader(actualServer); // 4

 

RWHttpHeaderList headerlist;

headerlist.addHeader(hostHeader); // 5

 

RWHttpRequest request(RWHttpRequest::Get, path, headerlist); // 6

 

client.submit(request); // 7

 

// retrieve reply from the server...

// 1  Constructs an RWCString that identifies the proxy HTTP server.

// 2   Constructs an RWCString that identifies the actual HTTP server.

// 3   Attempts to connect to the proxy HTTP server.

// 4   Constructs a Host header with the actual HTTP server as its value.

// 5   Adds the Host header to an RWHttpHeaderList object.

// 6   Constructs an RWHttpRequest object to GET the root document from a server. The RWHttpRequest object contains an additional header that identifies the actual target server that the document should be retrieved from.

// 7   Submits the request for the actual HTTP server to the RWHttpClient connected to the proxy HTTP server.

Issuing an HTTP request through a proxy HTTP server using the RWHttpAgent classes uses a slightly different process for specifying the proxy and actual servers to use:

 

RWHttpAgent agent;

 

agent.executeGet(

"http://proxy.somehost.com/http://www.perforce.com/");