File tree Expand file tree Collapse file tree 6 files changed +136
-0
lines changed
core-java/src/main/java/com/baeldung/networking/proxies Expand file tree Collapse file tree 6 files changed +136
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .baeldung .networking .proxies ;
2+
3+ import java .net .URL ;
4+ import java .net .URLConnection ;
5+
6+ public class CommandLineProxyDemo {
7+
8+ public static final String RESOURCE_URL = "http://www.google.com" ;
9+
10+ public static void main (String [] args ) throws Exception {
11+
12+ URL url = new URL (RESOURCE_URL );
13+ URLConnection con = url .openConnection ();
14+ System .out .println (UrlConnectionUtils .contentAsString (con ));
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .networking .proxies ;
2+
3+ import java .io .IOException ;
4+ import java .net .HttpURLConnection ;
5+ import java .net .Proxy ;
6+ import java .net .URL ;
7+
8+ public class DirectProxyDemo {
9+
10+ private static final String URL_STRING = "http://www.google.com" ;
11+
12+ public static void main (String ... args ) throws IOException {
13+
14+ URL weburl = new URL (URL_STRING );
15+ HttpURLConnection directConnection
16+ = (HttpURLConnection ) weburl .openConnection (Proxy .NO_PROXY );
17+ System .out .println (UrlConnectionUtils .contentAsString (directConnection ));
18+ }
19+
20+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .networking .proxies ;
2+
3+ import java .io .IOException ;
4+ import java .net .HttpURLConnection ;
5+ import java .net .InetSocketAddress ;
6+ import java .net .Proxy ;
7+ import java .net .Socket ;
8+ import java .net .URL ;
9+
10+ public class SocksProxyDemo {
11+
12+ private static final String URL_STRING = "http://www.google.com" ;
13+ private static final String SOCKET_SERVER_HOST = "someserver.baeldung.com" ;
14+ private static final int SOCKET_SERVER_PORT = 1111 ;
15+
16+ public static void main (String ... args ) throws IOException {
17+
18+ URL weburl = new URL (URL_STRING );
19+ Proxy socksProxy
20+ = new Proxy (Proxy .Type .SOCKS , new InetSocketAddress ("127.0.0.1" , 1080 ));
21+ HttpURLConnection socksConnection
22+ = (HttpURLConnection ) weburl .openConnection (socksProxy );
23+ System .out .println (UrlConnectionUtils .contentAsString (socksConnection ));
24+
25+ Socket proxySocket = new Socket (socksProxy );
26+ InetSocketAddress socketHost
27+ = new InetSocketAddress (SOCKET_SERVER_HOST , SOCKET_SERVER_PORT );
28+ proxySocket .connect (socketHost );
29+ // do stuff with the socket
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .networking .proxies ;
2+
3+ import java .net .URL ;
4+ import java .net .URLConnection ;
5+
6+ public class SystemPropertyProxyDemo {
7+
8+ public static final String RESOURCE_URL = "http://www.google.com" ;
9+
10+ public static void main (String [] args ) throws Exception {
11+
12+ System .setProperty ("http.proxyHost" , "127.0.0.1" );
13+ System .setProperty ("http.proxyPort" , "3128" );
14+
15+ URL url = new URL (RESOURCE_URL );
16+ URLConnection con = url .openConnection ();
17+ System .out .println (UrlConnectionUtils .contentAsString (con ));
18+
19+ System .setProperty ("http.proxyHost" , null );
20+ // proxy will no longer be used for http connections
21+ }
22+
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .networking .proxies ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+ import java .net .URLConnection ;
7+
8+ class UrlConnectionUtils {
9+
10+ public static String contentAsString (URLConnection con ) throws IOException {
11+ StringBuilder builder = new StringBuilder ();
12+ try (BufferedReader reader
13+ = new BufferedReader (new InputStreamReader (con .getInputStream ()))){
14+ while (reader .ready ()) {
15+ builder .append (reader .readLine ());
16+ }
17+ }
18+ return builder .toString ();
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .networking .proxies ;
2+
3+ import java .io .IOException ;
4+ import java .net .HttpURLConnection ;
5+ import java .net .InetSocketAddress ;
6+ import java .net .Proxy ;
7+ import java .net .URL ;
8+
9+ public class WebProxyDemo {
10+
11+ private static final String URL_STRING = "http://www.google.com" ;
12+
13+ public static void main (String ... args ) throws IOException {
14+
15+ URL weburl = new URL (URL_STRING );
16+ Proxy webProxy
17+ = new Proxy (Proxy .Type .HTTP , new InetSocketAddress ("127.0.0.1" , 3128 ));
18+ HttpURLConnection webProxyConnection
19+ = (HttpURLConnection ) weburl .openConnection (webProxy );
20+ System .out .println (UrlConnectionUtils .contentAsString (webProxyConnection ));
21+ }
22+
23+ }
You can’t perform that action at this time.
0 commit comments