标签:建议 rgb com tunnel uid rect 异常 creat 封装
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);
// 初始化proxy对象 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); // 创建连接 URL u = new URL(url); URLConnection conn = u.openConnection(proxy);
String headerKey = "Proxy-Authorization"; String encoded = new String(Base64.encodeBase64((new String(proxyUser + ":" + proxyPass).getBytes()))); String headerValue = "Basic " + encoded; conn.setRequestProperty(headerKey, headerValue);
public static class MyAuthenticator extends Authenticator { String userName; String password; public MyAuthenticator (String userName, String password) { this.userName = userName; this.password = password; } /** * 当须要使用密码校验时自己主动触发 */ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password.toCharArray()); } }
MyAuthenticator auth = new MyAuthenticator(proxyUser, proxyPass); Authenticator.setDefault(auth);
/** * 网络代理測试 * * <pre> * 设置代理主机及port:系统变量(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 :===================\n " + 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()); } }
标签:建议 rgb com tunnel uid rect 异常 creat 封装
原文地址:http://www.cnblogs.com/gccbuaa/p/7086416.html