BAEL-2174 How to use proxies in core java (#5805)

* move samples from core-java to new core-java-net module
* remove erroneous commit of .vscode dir
This commit is contained in:
chrisoberle
2018-12-02 21:23:39 -05:00
committed by KevinGilmore
parent 857192358c
commit 6b29e94a40
11 changed files with 63 additions and 1 deletions

View File

@@ -1,17 +0,0 @@
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

@@ -1,20 +0,0 @@
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

@@ -1,32 +0,0 @@
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

@@ -1,23 +0,0 @@
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

@@ -1,21 +0,0 @@
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

@@ -1,23 +0,0 @@
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));
}
}