File tree Expand file tree Collapse file tree
src/main/java/org/codejive/tproxy Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,9 +24,7 @@ A lightweight HTTP/HTTPS proxy library with pluggable interceptors and full MITM
2424HttpProxy proxy = new HttpProxy ();
2525proxy. start(8080 );
2626
27- HttpClient client = HttpClient . newBuilder()
28- .proxy(ProxySelector . of(new InetSocketAddress (" localhost" , 8080 )))
29- .build();
27+ HttpClient client = proxy. configureClient(HttpClient . newBuilder()). build();
3028
3129HttpResponse<String > response = client. send(
3230 HttpRequest . newBuilder(). uri(URI . create(" http://example.com" )). build(),
@@ -117,8 +115,7 @@ tmf.init(trustStore);
117115SSLContext sslContext = SSLContext . getInstance(" TLS" );
118116sslContext. init(null , tmf. getTrustManagers(), null );
119117
120- HttpClient client = HttpClient . newBuilder()
121- .proxy(ProxySelector . of(new InetSocketAddress (" localhost" , 8080 )))
118+ HttpClient client = proxy. configureClient(HttpClient . newBuilder())
122119 .sslContext(sslContext)
123120 .build();
124121```
Original file line number Diff line number Diff line change 11package org .codejive .tproxy ;
22
33import java .io .IOException ;
4+ import java .net .InetSocketAddress ;
5+ import java .net .ProxySelector ;
46import java .net .http .HttpClient ;
57import java .net .http .HttpRequest ;
68import java .net .http .HttpRequest .BodyPublishers ;
@@ -282,6 +284,20 @@ public int port() {
282284 return port ;
283285 }
284286
287+ /**
288+ * Configure an HttpClient.Builder to use this proxy.
289+ *
290+ * @param builder the HttpClient.Builder to configure
291+ * @return the same builder instance for chaining
292+ * @throws IllegalStateException if the proxy is not running
293+ */
294+ public HttpClient .Builder configureClient (HttpClient .Builder builder ) {
295+ if (!running ) {
296+ throw new IllegalStateException ("Proxy must be running before configuring clients" );
297+ }
298+ return builder .proxy (ProxySelector .of (new InetSocketAddress ("localhost" , port )));
299+ }
300+
285301 private static HttpClient buildHttpClient (SSLContext sslContext ) {
286302 HttpClient .Builder builder =
287303 HttpClient .newBuilder ()
You can’t perform that action at this time.
0 commit comments