实例代码
  入口类
/**
* 网络代理测试
*
* <pre>
* 设置代理主机及端口:系统变量(https 需同步设置)
* 设置代理验证方式:全局代理对象
*
*
* https链接错误:
* Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"
* 使用全局代理验证解决
*
* </pre>
*
* @author tzz
* @createDate 2015年7月23日
*
*/
public class ProxyTest {
private static String proxyHost = "xxx.xxxxx.com";
private static int proxyPort = 8080;
private static String proxyUser = "user";
private static String proxyPass = "pass";
public static void main(String[] args) {
String url = "https://www.google.com/";
String content = doProxy(url);
System.out.println("Result :=================== " + content);
}
/**
* 通过系统变量方式实现代理
*
* @param url
* @return
*/
public static String doProxy(String url) {
// 设置系统变量
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", "" + proxyPort);
// 针对https也开启代理
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", "" + proxyPort);
// 设置默认校验器
setDefaultAuthentication();
//开始请求
try {
URL u = new URL(url);
URLConnection conn = u.openConnection();
HttpsURLConnection httpsCon = (HttpsURLConnection) conn;
httpsCon.setFollowRedirects(true);
String encoding = conn.getContentEncoding();
if (StringUtils.isEmpty(encoding)) {
encoding = "UTF-8";
}
InputStream is = conn.getInputStream();
String content = IOUtils.toString(is, encoding);
return content;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
/**
* 设置全局校验器对象
*/
public static void setDefaultAuthentication() {
BasicAuthenticator auth = new BasicAuthenticator(proxyUser, proxyPass);
Authenticator.setDefault(auth);
}
}
  校验器
/**
* 实现sun.net的代理验证
*
* @author tzz
* @createDate 2015年7月23日
*
*/
public static class BasicAuthenticator extends Authenticator {
String userName;
String password;
public BasicAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
/**
* Called when password authorization is needed. Subclasses should override the default implementation, which returns null.
*
* @return The PasswordAuthentication collected from the user, or null if none is provided.
*/
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//System.out.println("DEBUG === use global authentication of password");
return new PasswordAuthentication(userName, password.toCharArray());
}
}
  常见问题
  连接时异常
  Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"
  通常是代理服务器未能读取到验证信息所致,请检查目标url是否为https连接以及全局的Authenticator类是否正确设置。