JavaWeb project requests network resources through a proxy server

1. Demand

For a JavaWeb project deployed on an intranet server, the backend needs to access the Internet, and Internet access is achieved through the proxy forwarding of another server with network access rights.

2. Realize

Intranet device - set to A, through external network device - set to B, to access the Internet, proxy configuration needs to be performed on A and B respectively. At this time, their relative relationship is that A is the client, B is the server, all Internet requests of A are sent to B, and B is forwarded to the target site on behalf of them - set to X. The data returned by X is also forwarded to A on behalf of B.

Back-end Java programs generally use apache's httpClient to request network resources. According to the information, the configuration methods of httpClient using proxy server to make network requests can be roughly divided into two types:

1. Configure httpClient parameters;

2. Configure the JVM system agent (note, not the operating system).

The second possibility is mainly used in development environments, such as IDEA or eclipse, etc. The first method is mainly discussed here.

Configure httpClient parameters

1. Minimalist Mode

// proxyHost -- proxy ip; proxyPort -- proxy port number; HTTP -- communication protocol
HttpHost httpProxy = new HttpHost(proxyHost, proxyPort, "HTTP");
CloseableHttpClient httpClient = HttpClients.createDefault();
// url -- resource path
HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity("message sent", "utf-8"));
CloseableHttpResponse response = httpClient.execute(httpProxy, request);

"Pay attention to this method: new HttpHost() supports the specified protocol type, but httpClient does not support the socks protocol by default, so if we change the protocol in the above code to new HttpHost("proxyHost", proxyPort, "SOCKS" ), an error will be reported." - Quoted from: Richard_Easy 2018.03.15

2. Official recommendation

A. Use the RequestConfig configuration class:

// proxyHost -- proxy ip; proxyPort -- proxy port number; HTTP -- communication protocol
HttpHost httpProxy = new HttpHost(proxyHost, proxyPort, "HTTP");
// url -- resource address
HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity("message sent", "utf-8"));
CloseableHttpClient httpClient = HttpClients.createDefault();
// configUtils -- custom configuration class object
RequestConfig requestConfig = RequestConfig.custom()
        .setConnectionRequestTimeout(configUtils.getConnectTimeout())
        .setConnectTimeout(configUtils.getConnectTimeout())
        .setSocketTimeout(configUtils.getSocketTimeout())
        .setProxy(configUtils.isUseProxy() ? httpProxy : null)
        .build();
request.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(request);

B. Using the RoutePlanner class:

How to set default parameters

// proxyHost -- proxy ip; proxyPort -- proxy port number
HttpHost httpProxy = new HttpHost("proxyHost", "proxyPort"); 
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(httpProxy); 
CloseableHttpClient httpclient = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();

Note: There is another implementation of "using system proxy" in this method, which will not be described here, refer to link 1 below.

 

In addition, when using httpClient to make https requests, there may be problems with SSL certificate identification, which can be solved by referring to the link 2 below.

3. References

1.The main setting method of httpClient using proxy

2.httpClient settings ignore SSL

Tags: Java http HttpClient

Posted by ankhmor on Mon, 30 May 2022 13:52:24 +0530