Using Java, I am attempting to connect and fetch the response code from http://www.oracle.com via a local proxy http://www-my.proxy.address.com.
I've tried:
public void testAConnection() throws IOException {
String urlText = "http://www.oracle.com";
System.setProperty("http.proxyHost", "http://www-my.proxy.address.com");
System.setProperty("http.proxyPort", "80");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("http://www-my.proxy.address.com", 80));
URL url = new URL(urlText);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
int responseCode = conn.getResponseCode();
System.out.println(responseCode);
}
Which throws:
java.net.UnknownHostException: ...
As well as:
Proxy proxy = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress("http://www-my.proxy.address.com", 80));
URL url = new URL(urlText);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
The latter throws:
ava.lang.IllegalArgumentException: type DIRECT is not compatible with address ...
For others who have solved this problem, the second method seems to usually work. Any help appreciated!
EDIT: After racking my brains trying to figure what was wrong, I restarted my IDE and everything seems to be working. Thanks for your feedback.
Type.HTTP is correct. But, I doubt whether your system is not able to resolve your proxy IP. Check your DNS settings or manually add entry in /etc/hosts for www-my.proxy.address.com