標籤:建議 rgb com tunnel uid rect 異常 creat 封裝
需求描寫敘述 在正常的項目開發需求中。串連遠程server的情境一般有二: 1 自家實現的httpserver,api介面都已經約定好。 2 開發平台服務。通常如新浪、百度雲等平台提供的restful介面。 以上的兩種情境通過原生的URLConnection或是apache提供的httpclient工具包都能夠方便的實現調用。
然而,第三種情境是須要串連國外的開放服務,如google、twitter、tumblr等開放API介面。
在偉大的gfw關懷下,我們被告知不要隨便和陌生人說話... 好吧,接下來讓我們開始實現基於proxy的穿越吧!
準備工作 1 http代理server 建議花點銀子買個穩定的VPN,帶http代理的那種。 2 外網訪問測試 能夠用chrome switchyOmega外掛程式測試一把。不行直接設定IE系統代理程式 準備完成。能夠開始開發了 設計分析 代理串連實現的關鍵步驟: 一、設定代理server地址port 方式一:Java支援以System.setProperty的方式設定http代理及port,例如以下:
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);
關於Java屬性的具體設定可參考:http://docs.oracle.com/javase/6/docs/technotes/guides/net/properties.html 方式二:使用Proxy對象,在建立串連時注入到URLConnection就可以:
// 初始化proxy對象Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); // 建立串連URL u = new URL(url);URLConnection conn = u.openConnection(proxy);
關於兩種方式的比較 第一種方式更值得推薦,當你採用基於URLConnection封裝實現的類庫時,採用setProperty的方式則不須要動裡面的代碼,綠色輕便。
二、實現使用者password校正 方式一:將校正資訊寫入http頭,將usernamepassword進行base64編碼之後設定Proxy-Authorization頭:
String headerKey = "Proxy-Authorization";String encoded = new String(Base64.encodeBase64((new String(proxyUser + ":" + proxyPass).getBytes())));String headerValue = "Basic " + encoded;conn.setRequestProperty(headerKey, headerValue);
不少資料會推薦這種方式。但經過測試,該方式在https的需求情境下無法正常工作!
方式二:實現Authenticator介面。並注入為全域驗證器:
public static class MyAuthenticator extends Authenticator { String userName; String password; public MyAuthenticator (String userName, String password) { this.userName = userName; this.password = password;} /*** 當須要使用密碼校正時自己主動觸發*/@Overrideprotected 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()); } }常見問題 串連時異常 Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required" 一般是代理server未能讀取到驗證資訊所致,請檢查目標url是否為https串連以及全域的Authenticator類是否正確設定。
開發技巧-Java通過HttpProxy實現穿越