public static Proxy getProxy() {
Authenticator.setDefault(new MyAuthenticator("账号", "密码"));
SocketAddress sa = new InetSocketAddress("代理Ip", "代理端口号");
Proxy proxy = new Proxy(Proxy.Type.HTTP, sa);
return proxy;
}
public static class MyAuthenticator extends Authenticator {
private String user;
private String password;
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
在配置类中加上这串代码就行