在前面的HTTP网络通信的例子中,使用了URLConnection conn = url.openConnection();连接网络,
如果改用URLConnection conn = url.openConnection(proxy);方式,传入一个proxy对象,设置好代理IP和端口,则可以实现代理连接,
下面是一个简单例子,
1 package proxy; 2 3 import java.io.IOException; 4 import java.io.PrintStream; 5 import java.net.InetSocketAddress; 6 import java.net.MalformedURLException; 7 import java.net.Proxy; 8 import java.net.URL; 9 import java.net.URLConnection; 10 import java.util.Scanner; 11 12 public class ProxyTest { 13 final String PROXY_ADDR = "172.20.230.5"; 14 final int PROXY_PORT = 3128; 15 String urlStr = "http://www.baidu.com"; 16 //String urlStr = "http://www.crazyit.org"; 17 18 public void init() throws IOException { 19 URL url = new URL(urlStr); 20 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_ADDR, PROXY_PORT)); 21 //使用代理服务器打开链接 22 URLConnection conn = url.openConnection(proxy); 23 //URLConnection conn = url.openConnection(); 24 conn.setConnectTimeout(5000); 25 try { 26 Scanner scan = new Scanner(conn.getInputStream()); 27 PrintStream ps = new PrintStream("index.html"); 28 while (scan.hasNextLine()) { 29 String line = scan.nextLine(); 30 System.out.println(line); 31 ps.println(line); 32 } 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } 36 } 37 38 public static void main(String[] args) throws IOException { 39 new ProxyTest().init(); 40 } 41 }
实现自动代理
在上面的例子中,每次用url对象open一个connection的时候,都需要显示地传入一个proxy对象才行。
而实际上可以在connection之前,做一个默认代理设置,这样以后再openConnection的时候,就不需要显示传入proxy对象了。
做默认代理设置需要重写ProxySelector的select方法,返回代理IP和端口列表,具体实现如下,
1 package proxy; 2 3 import java.io.IOException; 4 import java.io.PrintStream; 5 import java.net.InetSocketAddress; 6 import java.net.MalformedURLException; 7 import java.net.Proxy; 8 import java.net.ProxySelector; 9 import java.net.SocketAddress; 10 import java.net.URI; 11 import java.net.URL; 12 import java.net.URLConnection; 13 import java.util.ArrayList; 14 import java.util.List; 15 import java.util.Scanner; 16 17 public class ProxyTest { 18 final String PROXY_ADDR = "172.20.230.5"; 19 final int PROXY_PORT = 3128; 20 String urlStr = "http://www.baidu.com"; 21 //String urlStr = "http://www.crazyit.org"; 22 23 public void init() throws IOException { 24 ProxySelector.setDefault(new ProxySelector(){ 25 26 @Override 27 public void connectFailed(URI arg0, SocketAddress arg1, 28 IOException arg2) { 29 System.out.println("无法连接到服务器"); 30 31 } 32 33 @Override 34 public List<Proxy> select(URI uri) { 35 List<Proxy> result = new ArrayList<>(); 36 result.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_ADDR, PROXY_PORT))); 37 return result; 38 } 39 40 }); 41 42 URL url = new URL(urlStr); 43 //使用代理服务器打开链接 44 URLConnection conn = url.openConnection(); 45 conn.setConnectTimeout(5000); 46 try { 47 Scanner scan = new Scanner(conn.getInputStream()); 48 PrintStream ps = new PrintStream("index.html"); 49 while (scan.hasNextLine()) { 50 String line = scan.nextLine(); 51 System.out.println(line); 52 ps.println(line); 53 } 54 } catch (IOException e) { 55 e.printStackTrace(); 56 } 57 } 58 59 public static void main(String[] args) throws IOException { 60 new ProxyTest().init(); 61 } 62 }
可以看到使用代理之后,再用url打开链接时就能像普通连接那样url.openConnection();了