Using Java Proxy Configuration to Access Internal Services from Outside the Network
The article explains how to bypass internal network restrictions by configuring a Java HTTP proxy, provides demo code for setting and using the proxy, and includes utility methods for proxy validation and request configuration.
During testing, the author encountered a restriction where certain services could only be accessed from the data‑center network segment, causing inconvenience for file synchronization and functional testing.
To overcome this, a Java proxy configuration is proposed: set up an HTTP proxy on a development machine in the data center, retrieve the proxy address, and route requests through it to bypass the internal network.
Demo code shows how to send an HTTP POST request, set the proxy, and output the response, including sample console logs with JSON results from ip‑api.com.
public static void main(String[] args) {
def get = getHttpPost("http://ip-api.com/json/?lang=zh-CN ")
def response1 = getHttpResponse(get)
output(response1)
setProxy(get,"104.129.198.211:10605")
def response = getHttpResponse(get)
output(response)
testOver()
}Console output demonstrates successful requests to the external API before and after applying the proxy.
Utility methods are provided to validate the proxy address format, create a RequestConfig with proxy settings, and overloads for specifying the proxy as IP and port.
/**
* 设置代理请求
*
* @param request
* @param adress
*/
public static void setProxy(HttpRequestBase request, String adress) {
if (!Regex.isMatch(adress, "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))):([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{4}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])"))
ParamException.fail("adress格式错误:" + adress);
String[] split = adress.split(":");
RequestConfig proxyRequestConfig = ClientManage.getProxyRequestConfig(split[0], changeStringToInt(split[1]));
request.setConfig(proxyRequestConfig);
}
public static void setProxy(HttpRequestBase request, String ip, int port) {
setProxy(request, ip + ":" + port);
}
/**
* 获取代理配置项
*
* @param ip
* @param port
* @return
*/
public static RequestConfig getProxyRequestConfig(String ip, int port) {
return RequestConfig.custom()
.setConnectionRequestTimeout(HttpClientConstant.CONNECT_REQUEST_TIMEOUT)
.setConnectTimeout(HttpClientConstant.CONNECT_TIMEOUT)
.setSocketTimeout(HttpClientConstant.SOCKET_TIMEOUT)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.setRedirectsEnabled(false)
.setProxy(new HttpHost(ip, port))
.build();
}For the full source code, visit the GitHub repository https://github.com/JunManYuanLong/FunTester and refer to the original article links for additional context.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
