BAEL-2174: proxies in core java (#5628)

This commit is contained in:
chrisoberle 2018-11-17 13:24:21 -05:00 committed by KevinGilmore
parent 267f287f01
commit 301ffdbc75
6 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.networking.proxies;
import java.net.URL;
import java.net.URLConnection;
public class CommandLineProxyDemo {
public static final String RESOURCE_URL = "http://www.google.com";
public static void main(String[] args) throws Exception {
URL url = new URL(RESOURCE_URL);
URLConnection con = url.openConnection();
System.out.println(UrlConnectionUtils.contentAsString(con));
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.networking.proxies;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
public class DirectProxyDemo {
private static final String URL_STRING = "http://www.google.com";
public static void main(String... args) throws IOException {
URL weburl = new URL(URL_STRING);
HttpURLConnection directConnection
= (HttpURLConnection) weburl.openConnection(Proxy.NO_PROXY);
System.out.println(UrlConnectionUtils.contentAsString(directConnection));
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.networking.proxies;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.URL;
public class SocksProxyDemo {
private static final String URL_STRING = "http://www.google.com";
private static final String SOCKET_SERVER_HOST = "someserver.baeldung.com";
private static final int SOCKET_SERVER_PORT = 1111;
public static void main(String... args) throws IOException {
URL weburl = new URL(URL_STRING);
Proxy socksProxy
= new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1080));
HttpURLConnection socksConnection
= (HttpURLConnection) weburl.openConnection(socksProxy);
System.out.println(UrlConnectionUtils.contentAsString(socksConnection));
Socket proxySocket = new Socket(socksProxy);
InetSocketAddress socketHost
= new InetSocketAddress(SOCKET_SERVER_HOST, SOCKET_SERVER_PORT);
proxySocket.connect(socketHost);
// do stuff with the socket
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.networking.proxies;
import java.net.URL;
import java.net.URLConnection;
public class SystemPropertyProxyDemo {
public static final String RESOURCE_URL = "http://www.google.com";
public static void main(String[] args) throws Exception {
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "3128");
URL url = new URL(RESOURCE_URL);
URLConnection con = url.openConnection();
System.out.println(UrlConnectionUtils.contentAsString(con));
System.setProperty("http.proxyHost", null);
// proxy will no longer be used for http connections
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.networking.proxies;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;
class UrlConnectionUtils {
public static String contentAsString(URLConnection con) throws IOException {
StringBuilder builder = new StringBuilder();
try (BufferedReader reader
= new BufferedReader(new InputStreamReader(con.getInputStream()))){
while (reader.ready()) {
builder.append(reader.readLine());
}
}
return builder.toString();
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.networking.proxies;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
public class WebProxyDemo {
private static final String URL_STRING = "http://www.google.com";
public static void main(String... args) throws IOException {
URL weburl = new URL(URL_STRING);
Proxy webProxy
= new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
HttpURLConnection webProxyConnection
= (HttpURLConnection) weburl.openConnection(webProxy);
System.out.println(UrlConnectionUtils.contentAsString(webProxyConnection));
}
}