Java Proxy Configuration Examples
Apache HttpClient
The example below shows how to use ProxyMesh Custom Headers with Apache HttpClient, by extending the HttpRequestExecutor
class. You should add your IP to ProxyMesh for IP authentication.
Set your httpclient to use this custom executor class. The HttpContext
should be used to set any other meta information needed.
For the https headers, the preProcess
method is used to insert them where doReceiveResponse
will capture them.
If you are persisting connections, then you'll have to force the client to not keep the connection alive so the preProcess
method will be called again to insert the header.
doSendRequest
method.
import org.apache.http.*; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpRequestExecutor; import java.io.IOException; public class CustomHttpRequestExecutor extends HttpRequestExecutor { private final String proxyIpAttributeId = "proxy:ip"; @Override public void preProcess(HttpRequest request, HttpProcessor processor, HttpContext context) throws IOException, HttpException { HttpClientContext clientContext = HttpClientContext.adapt(context); String proxyIp = clientContext.getAttribute(proxyIpAttributeId, String.class); // used for HTTPS requests if(proxyIp != null && "CONNECT".equalsIgnoreCase(request.getRequestLine().getMethod())) { request.setHeader("X-ProxyMesh-IP", proxyIp); } super.preProcess(request, processor, context); } @Override protected HttpResponse doSendRequest(HttpRequest request, HttpClientConnection conn, HttpContext context) throws IOException, HttpException { HttpClientContext clientContext = HttpClientContext.adapt(context); String proxyIp = clientContext.getAttribute(proxyIpAttributeId, String.class); // used for HTTP requests boolean isHttps = true; // set something on your context to see if https was used if(!isHttps && proxyIp != null) { request.setHeader("X-ProxyMesh-IP", proxyIp); } return super.doSendRequest(request, conn, context); } @Override protected HttpResponse doReceiveResponse(HttpRequest request, HttpClientConnection conn, HttpContext context) throws HttpException, IOException { HttpClientContext clientContext = HttpClientContext.adapt(context); HttpResponse response = super.doReceiveResponse(request, conn, context); Header proxyIpNotFoundHeader = response.getFirstHeader("X-ProxyMesh-IP-Not-Found"); Header proxyIpHeader = response.getFirstHeader("X-ProxyMesh-IP"); if(proxyIpNotFoundHeader != null) { System.err.println("Proxy IP not found!"); } else if(proxyIpHeader != null) { String proxyIp = proxyIpHeader.getValue(); System.out.printf("Proxy IP received: [%s]", proxyIp); clientContext.setAttribute(proxyIpAttributeId, proxyIp); } return response; } } } }
Custom Headers with HTTPS Requests
This section describes ways to do a CONNECT over HTTPS, with custom headers added. You may also wish to review our Proxy Server Requests over HTTPS with particular reference to the X-ProxyMesh-IP
header. Note that in the request custom headers are injected immediately after the CONNECT command.
Similarly, in the final response, the proxy server cannot inject an extra header. Instead, the X-ProxyMesh-IP
response header is injected immediately after the Connection response.
package org.apache.http.examples.client; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.Socket; import org.apache.http.HttpHost; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.impl.client.ProxyClient; import org.apache.http.protocol.HTTP; /** * Example code for using {@link ProxyClient} in order to establish a tunnel through an HTTP proxy. */ public class ProxyTunnelDemo { public final static void main(String[] args) throws Exception { ProxyClient proxyClient = new ProxyClient(); HttpHost target = new HttpHost("www.yahoo.com", 80); HttpHost proxy = new HttpHost("PROXYHOST", PORT); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd"); Socket socket = proxyClient.tunnel(proxy, target, credentials); try { Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET); out.write("GET / HTTP/1.1\r\n"); out.write("Host: " + target.toHostString() + "\r\n"); out.write("Agent: whatever\r\n"); out.write("Connection: close\r\n"); out.write("\r\n"); out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET)); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { socket.close(); } } } } } }
Selenium
To use Selenium and Java with ProxyMesh, see these 2 links for examples: