现在的位置: 首页 > 综合 > 正文

java中设置代理的两种方式

2018年01月24日 ⁄ 综合 ⁄ 共 3047字 ⁄ 字号 评论关闭
方法一:采用设置系统属性
 
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.Properties;
 
public class ProxyDemo1 {
    public static void main(String[] args) {
        Properties prop = System.getProperties();
        // 设置http访问要使用的代理服务器的地址
        prop.setProperty("http.proxyHost", "183.45.78.31");
        // 设置http访问要使用的代理服务器的端口
        prop.setProperty("http.proxyPort", "8080");
        // 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
        prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
        // 设置安全访问(https)使用的代理服务器地址与端口,它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问,对于https的,直接设置成如下方式:
        prop.setProperty("proxyHost", "183.45.78.31");
        prop.setProperty("proxyPort", "443");
        // 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
        prop.setProperty("ftp.proxyHost", "183.45.78.31");
        prop.setProperty("ftp.proxyPort", "21");
        prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
        // socks代理服务器的地址与端口
        prop.setProperty("socksProxyHost", "183.45.78.31");
        prop.setProperty("socksProxyPort", "1080");
        // 设置登陆到代理服务器的用户名和密码
        Authenticator.setDefault(new MyAuthenticator("userName", "Password"));
// 请求地址:
URL url = new URL("http://www.baidu.com");
        URLConnection  connection = url.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()));
String inputLine;
          while (
            (inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
          }
    }
 
    static class MyAuthenticator extends Authenticator {
        private String user = "";
        private String password = "";
 
        public MyAuthenticator(String user, String password) {
            this.user = user;
            this.password = password;
        }
 
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password.toCharArray());
        }
    }
 
}
 
 
方法二:使用Proxy
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
 
public class ProxyDemo2 {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.3lai8.com");
        // /创建代理服务器
        InetSocketAddress addr = new InetSocketAddress("192.168.0.254", 8080);
        // Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
        Authenticator.setDefault(new MyAuthenticator("username", "password"));// 设置代理的用户和密码
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);// 设置代理访问
        InputStreamReader in = new InputStreamReader(connection.getInputStream());
        BufferedReader reader = new BufferedReader(in);
        while (true) {
            String s = reader.readLine();
            if (s != null) {
                System.out.println(s);
            }
        }
    }
 
    static class MyAuthenticator extends Authenticator {
        private String user = "";
        private String password = "";
 
        public MyAuthenticator(String user, String password) {
            this.user = user;
            this.password = password;
        }
 
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password.toCharArray());
        }
    }
 
}

抱歉!评论已关闭.