JAVA基础知识之网络编程——-使用Proxy创建连接

在前面的HTTP网络通信的例子中,使用了URLConnection conn = url.openConnection();连接网络,

如果改用URLConnection conn = url.openConnection(proxy);方式,传入一个proxy对象,设置好代理IP和端口,则可以实现代理连接,

下面是一个简单例子,

 package proxy;

 import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner; public class ProxyTest {
final String PROXY_ADDR = "172.20.230.5";
final int PROXY_PORT = 3128;
String urlStr = "http://www.baidu.com";
//String urlStr = "http://www.crazyit.org"; public void init() throws IOException {
URL url = new URL(urlStr);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_ADDR, PROXY_PORT));
//使用代理服务器打开链接
URLConnection conn = url.openConnection(proxy);
//URLConnection conn = url.openConnection();
conn.setConnectTimeout(5000);
try {
Scanner scan = new Scanner(conn.getInputStream());
PrintStream ps = new PrintStream("index.html");
while (scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println(line);
ps.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
new ProxyTest().init();
}
}

实现自动代理

在上面的例子中,每次用url对象open一个connection的时候,都需要显示地传入一个proxy对象才行。

而实际上可以在connection之前,做一个默认代理设置,这样以后再openConnection的时候,就不需要显示传入proxy对象了。

做默认代理设置需要重写ProxySelector的select方法,返回代理IP和端口列表,具体实现如下,

 package proxy;

 import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class ProxyTest {
final String PROXY_ADDR = "172.20.230.5";
final int PROXY_PORT = 3128;
String urlStr = "http://www.baidu.com";
//String urlStr = "http://www.crazyit.org"; public void init() throws IOException {
ProxySelector.setDefault(new ProxySelector(){ @Override
public void connectFailed(URI arg0, SocketAddress arg1,
IOException arg2) {
System.out.println("无法连接到服务器"); } @Override
public List<Proxy> select(URI uri) {
List<Proxy> result = new ArrayList<>();
result.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_ADDR, PROXY_PORT)));
return result;
} }); URL url = new URL(urlStr);
//使用代理服务器打开链接
URLConnection conn = url.openConnection();
conn.setConnectTimeout(5000);
try {
Scanner scan = new Scanner(conn.getInputStream());
PrintStream ps = new PrintStream("index.html");
while (scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println(line);
ps.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
new ProxyTest().init();
}
}

可以看到使用代理之后,再用url打开链接时就能像普通连接那样url.openConnection();了

上一篇:01快速实现一个基于Jws的Webservice项目


下一篇:ASP.NETMVC 视图(二)