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:
committed by
KevinGilmore
parent
857192358c
commit
6b29e94a40
25
core-java-net/.gitignore
vendored
Normal file
25
core-java-net/.gitignore
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
*.class
|
||||
|
||||
0.*
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
.resourceCache
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea/
|
||||
*.iml
|
||||
3
core-java-net/README.md
Normal file
3
core-java-net/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
=========
|
||||
|
||||
## Core Java Net
|
||||
19
core-java-net/pom.xml
Normal file
19
core-java-net/pom.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-net</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-net</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-net</finalName>
|
||||
</build>
|
||||
</project>
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
13
core-java-net/src/test/resources/.gitignore
vendored
Normal file
13
core-java-net/src/test/resources/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
Reference in New Issue
Block a user