Skip to content

Instantly share code, notes, and snippets.

@AlainODea
Last active February 3, 2019 05:39
Show Gist options
  • Save AlainODea/6524217 to your computer and use it in GitHub Desktop.
Save AlainODea/6524217 to your computer and use it in GitHub Desktop.
Java Proxy Verfication - Simple URL Loader with no dependencies outside JavaSE
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
public class GetURL
{
public static void main(String[] args) throws Exception
{
URL url = URI.create(args[0]).toURL();
try (InputStream inputStream = url.openStream();
Reader reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader))
{
bufferedReader.lines().forEach(System.out::println);
}
}
}
@AlainODea
Copy link
Author

Compiling:

javac GetURL.java

Running with no proxy:

$ java GetURL https://example.com/
...
    <h1>Example Domain</h1>
....

Running with system proxies:

> java -Djava.net.useSystemProxies=true GetURL https://example.com/
...
    <h1>Example Domain</h1>
....

Running with an explicit HTTP proxy:

> java -Dhttps.proxyHost=localhost -Dhttps.proxyPort=3128 GetURL https://example.com/
...
    <h1>Example Domain</h1>
....

Running with an explicit SOCKS proxy:

> java -DsocksProxyHost=localhost -DsocksProxyPort=3128 GetURL https://example.com/
...
    <h1>Example Domain</h1>
....

Troubleshooting

Accidentally using a SOCKS proxy as an HTTP proxy fails like this:

Exception in thread "main" java.net.SocketException: Unexpected end of file from server
	at java.base/sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:862)
	at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:689)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2074)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1581)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
	at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
	at java.base/java.net.URL.openStream(URL.java:1117)
	at GetURL.main(GetURL.java:13)

Accidentally using the wrong port or host fails like this (assuming the host or port is unreachable):

Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
	at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at java.base/java.net.Socket.connect(Socket.java:540)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
	at java.base/sun.net.www.http.HttpClient$1.run(HttpClient.java:526)
	at java.base/sun.net.www.http.HttpClient$1.run(HttpClient.java:524)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/sun.net.www.http.HttpClient.privilegedOpenServer(HttpClient.java:523)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:564)
	at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
	at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1581)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
	at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
	at java.base/java.net.URL.openStream(URL.java:1117)
	at GetURL.main(GetURL.java:13)

Reference

Read more Proxies - Networking Properties on Oracle's documentation site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment