Skip to content

Instantly share code, notes, and snippets.

@ripla
Created December 22, 2016 08:04
Show Gist options
  • Save ripla/085a0d6bf34a076dc138931e7bda164a to your computer and use it in GitHub Desktop.
Save ripla/085a0d6bf34a076dc138931e7bda164a to your computer and use it in GitHub Desktop.
Simple URL connection checker for proxied connections
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.stream.Collectors;
public class UrlChecker {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println(
"Usage: " + UrlChecker.class.getName() + " " +
"<url> <proxy> <port>");
System.exit(1);
}
try {
String url = args[0];
String proxyHost = args[1];
int proxyPort = Integer.parseInt(args[2]);
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort));
URLConnection con = new URL(url).openConnection(proxy);
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
System.out.println(br.lines()
.collect(Collectors.joining(System.lineSeparator())));
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment