Created
December 2, 2021 16:59
-
-
Save JonasGroeger/a121eb4482eb814eb20eb692234b65dc to your computer and use it in GitHub Desktop.
JVM HTTP / HTTPS proxy configuration using jshell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.http.*; | |
import java.io.*; | |
var httpClient = HttpClient.newHttpClient(); | |
var httpProxyHost = System.getProperty("http.proxyHost"); | |
var httpProxyPort = System.getProperty("http.proxyPort"); | |
var httpsProxyHost = System.getProperty("https.proxyHost"); | |
var httpsProxyPort = System.getProperty("https.proxyPort"); | |
void run(URI uri, String testName) throws IOException, InterruptedException { | |
System.out.print("** " + testName + " "); | |
System.out.println("*".repeat(60)); | |
System.out.println("url = " + uri); | |
if(uri.toString().startsWith("https://")) { | |
System.out.println("httpsProxyHost = " + httpsProxyHost); | |
System.out.println("httpsProxyPort = " + httpsProxyPort); | |
} else { | |
System.out.println("httpProxyHost = " + httpProxyHost); | |
System.out.println("httpProxyPort = " + httpProxyPort); | |
} | |
var request = HttpRequest.newBuilder().uri(uri).build(); | |
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | |
System.out.println("\t-> " + response.statusCode()); | |
} | |
var httpUrl = new URI("http://httpbin.org/get"); | |
run(httpUrl, "HTTP GET"); | |
var httpsUrl = new URI("https://httpbin.org/get"); | |
run(httpsUrl, "HTTPS GET"); | |
/exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
HTTP_PROXY_HOST=localhost | |
HTTP_PROXY_PORT=9000 | |
HTTPS_PROXY_HOST=localhost | |
HTTPS_PROXY_PORT=9000 | |
jshell \ | |
proxy.jsh | |
jshell \ | |
-R-Dhttp.proxyHost="$HTTP_PROXY_HOST" \ | |
-R-Dhttp.proxyPort="$HTTP_PROXY_PORT" \ | |
-R-Dhttps.proxyHost="$HTTPS_PROXY_HOST" \ | |
-R-Dhttps.proxyPort="$HTTPS_PROXY_PORT" \ | |
proxy.jsh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment