您好,登錄后才能下訂單哦!
在Java中怎么利用代理對網絡進行連接?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
需求是這樣的:
一、界面上要有這樣幾種代理類型可以選。
1.HTTP代理
2.Socks代理
3.不使用代理(直連)
4.使用瀏覽器設置(瀏覽器也是HTTP、Socks、直連三種)。
可參考QQ登錄設置里的代理能,其實跟qq的代理功能是一樣的。
二、測試使用所填寫的代理配置信息是否可連接
三、記錄用戶上次選擇的代理配置,默認使用用戶上次使用的代理配置進行網絡連接。
程序運行環境是WindowsXP、Windows7、Windows8系統。
使用的技術為Java7,Swing,CXF。
難點:
1.如何進行全居的代理設置:
/** * 網絡代理Bean * * @author tang */ public class NetworkBean implements Serializable { private static final long serialVersionUID = 1L; // private static sun.misc.BASE64Encoder base64Encoder = new sun.misc.BASE64Encoder(); private Proxy.Type type;// 代理類型 private String address;// ip 地址 private String port;// 端口號 private String username;// 代理服務器用戶名 private String password;// 代理服務器用戶密碼 private String domain;// 域 private String typeText;// 代理類型顯示的文本 public NetworkBean() { } public NetworkBean(Type type, String address, String port, String username, String password) { this.type = type; this.address = address; this.port = port; this.username = username; this.password = password; } public NetworkBean(Type type, String address, String port, String username, String password, String domain) { super(); this.type = type; this.address = address; this.port = port; this.username = username; this.password = password; this.domain = domain; } public Proxy.Type getType() { return type; } public void setType(Proxy.Type type) { this.type = type; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDomain() { return domain; } public void setDomain(String domain) { this.domain = domain; } public String getTypeText() { return typeText; } public void setTypeText(String typeText) { this.typeText = typeText; } /** * return domain + "\" + username */ public String getDomainAndUsername() { return (Utils.toString(domain).trim().isEmpty()) ? username : domain.trim() + "\\" + username; } /** * return domain + "\" + username + ":" + password */ public String getDomainAndUsernameAndPassword() { return getDomainAndUsername() + ":" + password; } /** * return username + ":" + password */ public String getUsernameAndPassword() { return username + ":" + password; } /** * return (domain + "\" + username + ":" + password) to 64 bit */ public String getDomainAndUsernameAndPassword64() { return org.apache.commons.codec.binary.Base64.encodeBase64String(getDomainAndUsernameAndPassword().getBytes()); } @Override public String toString() { return "NetworkBean [type=" + type + ", typeText=" + typeText + ", address=" + address + ", port=" + port + ", username=" + username + ", password=" + password + ", domain=" + domain + "]"; } }
/** * 根據指定的代理信息設置系統全局的網絡代理 * * @param networkBean */ public static void setNetworkProxyBySystem(NetworkBean networkBean) { System.out.println("System Set Proxy : " + networkBean); if (isUserProxy(networkBean)) { if (networkBean.getType() == Proxy.Type.SOCKS) { System.getProperties().remove("http.proxyHost"); System.getProperties().remove("http.proxyPort"); System.getProperties().setProperty("socksProxyHost", networkBean.getAddress()); System.getProperties().setProperty("socksProxyPort", networkBean.getPort()); } else { System.getProperties().setProperty("http.proxyHost", networkBean.getAddress()); System.getProperties().setProperty("http.proxyPort", networkBean.getPort()); } Authenticator.setDefault(new BairuiAuthenticator(networkBean.getDomainAndUsername(), networkBean.getPassword())); } else if (networkBean != null) { System.getProperties().remove("proxySet"); System.getProperties().remove("socksProxySet"); System.getProperties().remove("http.proxyHost"); System.getProperties().remove("http.proxyPort"); System.getProperties().remove("socksProxyHost"); System.getProperties().remove("socksProxyPort"); } } /** * 網絡用戶名密碼校驗提供者 */ public static class BairuiAuthenticator extends Authenticator { private String username, password; public BairuiAuthenticator(String username, String password) { this.username = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password == null ? null : password.toCharArray()); } }
2.如何讓CXF的Service使用系統的代理:
/** * 為WebService接口添加網絡代理支持:httpClientPolicy.setAllowChunking(false); * * @param client */ public static void setWebServiceSupportProxy(Client client) { HTTPConduit conduit = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); httpClientPolicy.setAllowChunking(false); conduit.setClient(httpClientPolicy); }
3.使用瀏覽器設置:
/** * 讀取用戶注冊表獲取瀏覽器的代理設置 * * @return 該方法不會返回null值也不會拋出異常 */ public static NetworkBean getBrowserProxy() { NetworkBean networkBean = new NetworkBean(); networkBean.setTypeText(typeTexts[3]); networkBean.setType(Proxy.Type.DIRECT); String key = "reg query \"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\"";// 注冊表瀏覽器代理key try { Process exec = Runtime.getRuntime().exec(key); try (InputStreamReader i = new InputStreamReader(exec.getInputStream()); BufferedReader ir = new BufferedReader(i)) { for (String line = ir.readLine(); line != null; line = ir.readLine()) { if (line.indexOf("ProxyServer") >= 0) { String[] split1 = line.split(" "); if (split1.length > 3) { String[] split2 = split1[3].trim().split(":"); if (split2.length > 1) { if (!Utils.toString(split2[0]).isEmpty() && !Utils.toString(split2[1]).isEmpty()) { networkBean.setAddress(split2[0]); networkBean.setPort(split2[1]); networkBean.setType(Proxy.Type.HTTP); break; } } } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) {// 從注冊表讀取失敗 e.printStackTrace(); } return networkBean; }
4.測試代理是否可用
因為程序中使用了服務器的兩個不同的端口,所以需要測試兩個端口是否都可連。
如果使用了多臺服務器,更加需要分別測試了。
/** * 測試網絡代理是否能通過連接,如果不通過拋出異常 * * @throws Exception */ private static void testNetworkProxy() throws Exception { testWebService(); testURLConnection(); } /** * 測試CXF Service接口50367端口 * * @param networkBean * @throws Exception */ public static void testWebService() throws Exception { JcptLoginService service = WebServiceUtils.getService(JcptLoginService.class, GeneralWebServiceAddress.LOGIN_SERVICE_URL_ADD); String result = service.getLoginPicture(); System.out.println(result); } /** * 從HttpURLConnection對象讀取指定字符,如果不匹配則拋出異常 * * @param connection * @throws Exception */ private static void checkConnectionContent(HttpURLConnection connection) throws Exception { try (InputStream inputStream = connection.getInputStream()) { byte[] b = new byte[1024]; boolean success = false; StringBuffer sb = new StringBuffer(); for (int i = inputStream.read(b); i > 0; i = inputStream.read(b)) { String tempStr = new String(b, 0, i); sb.append(tempStr); if (tempStr.indexOf("3,file not found") >= 0) {// service固定返回這個字符串,如果service作了更改此處也應更改 success = true; break; } } if (!success) { String str = sb.toString(); if (str.length() > 3) { char char0 = str.charAt(0); char char1 = str.charAt(1); if (Utils.isNumber(char0 + "") && char1 == ',') { success = true; } } } if (!success) { throw new RuntimeException("result content does not meet expectations."); } } catch (Exception ex) { throw ex; } } /** * 測試文件下載接口9067端口 * * @param networkBean * @throws Exception */ public static void testURLConnection() throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(GeneralWebServiceAddress.FILE_DOWN_URL_ADD + "path=").openConnection(); checkConnectionContent(connection); }
/** * 測試代理服務器連接 */ private void testProxyConnection() { NetworkBean readNetworkBean = NetworkProxyTool.readNetworkBean();// 先獲得正在使用的NetworkBean try { NetworkBean networkBean = createNetworkBean();// 根據用戶填寫的信息創建的NetworkBean對象 showTestResultDialog(NetworkProxyTool.testNetworkProxyBySystem(networkBean)); } catch (Exception ex) { showTestResultDialog(false); } NetworkProxyTool.setNetworkProxyBySystem(readNetworkBean);// 測試結束,還原原來的NetworkBean }
5.因為java連接網絡時,如果使用當前的代理連接失敗,那么就會使用操作系統中緩存的代理進行網絡連接,如何是測試連接時不使用操作系統緩存,但測試結束后使用操作系統緩存。
/** * 設置長連接和驗證信息緩存是否開啟 * * @param keepAlive */ public static void setKeepAliveAndAuthCache(boolean keepAlive) { System.setProperty("http.keepAlive", keepAlive + "");// 設置是否開始長連接,如果為false可以防止連接被緩存(如果連接被緩存,用戶名密碼等所有信息都會被緩存) if (keepAlive) { AuthCacheValue.setAuthCache(new AuthCacheImpl()); } else { // 設置一個空的實現AuthCache可以防止用戶名密碼信息被緩存 AuthCacheValue.setAuthCache(new AuthCache() { public void remove(String pkey, AuthCacheValue entry) { } public void put(String pkey, AuthCacheValue value) { } public AuthCacheValue get(String pkey, String skey) { return null; } }); } }
在登錄之前:
NetworkProxyTool.setKeepAliveAndAuthCache(false); new.Login();
登錄成功后:
NetworkProxyTool.setKeepAliveAndAuthCache(true);
6.保存用戶的代理配置:
因為直接序列化自定義類型的對象,會存在版本問題(比如這個類的包名、類名改了,就死定了)。
所以不能直接序列化自定義類的對象,二是將自定義類對象的屬性轉成字典(Map),然后序列化map。
/** * 讀取本地網絡代理設置配置文件 * * @return */ public static NetworkBean readNetworkBean() { NetworkBean networkBean = getCurrNetworkBean(readNetworkBeanMap()); if (networkBean == null) { networkBean = new NetworkBean(); networkBean.setType(Proxy.Type.DIRECT); } return networkBean; } /** * 獲取用戶上次選擇網絡代理設置 * * @param map * @return */ public static NetworkBean getCurrNetworkBean(Map<String, Object> map) { putBrowserProxy(map); return (NetworkBean) map.get(getTypeMapKey(map)); } /** * 將瀏覽器的信息存放入代理信息總配置map * * @param map */ private static void putBrowserProxy(Map<String, Object> map) { if (browserProxyBean == null) { browserProxyBean = getBrowserProxy(); } NetworkBean networkBeanBrowser = (NetworkBean) map.get(key_browser); if (networkBeanBrowser == null) { networkBeanBrowser = browserProxyBean; } if ((Utils.toString(browserProxyBean.getAddress()).isEmpty() || !browserProxyBean.getAddress().equals(networkBeanBrowser.getAddress())) || (Utils.toString(browserProxyBean.getPort()).isEmpty() || !browserProxyBean.getPort().equals(networkBeanBrowser.getPort()))) { networkBeanBrowser.setUsername(null); networkBeanBrowser.setPassword(null); networkBeanBrowser.setDomain(null); } networkBeanBrowser.setType(browserProxyBean.getType()); networkBeanBrowser.setTypeText(browserProxyBean.getTypeText()); networkBeanBrowser.setAddress(browserProxyBean.getAddress()); networkBeanBrowser.setPort(browserProxyBean.getPort()); map.put(key_browser, networkBeanBrowser); }
在登錄之前:
NetworkBean networkBean = NetworkProxyTool.readNetworkBean(); NetworkProxyTool.setNetworkProxyBySystem(networkBean); NetworkProxyTool.setKeepAliveAndAuthCache(false); new.Login();
在用戶配置完代理點擊確定時:
/** * 點擊確定 */ private void confirm() { if ((isHttp() || isSocks()) && !checkIpAndPortNotNull()) { return; } NetworkBean networkBean = createNetworkBean(); if (isHttp()) {// HTTP代理 if (networkBean.getDomain() != null) { networkBean.setDomain(networkBean.getDomain().trim()); } proxySettingMap.put(key_http, networkBean); proxySettingMap.put(key_proxy_type, key_http); } else if (isSocks()) {// SOCKS代理 proxySettingMap.put(key_socks, networkBean); proxySettingMap.put(key_proxy_type, key_socks); } else if (isBrowser()) { proxySettingMap.put(key_browser, networkBean); proxySettingMap.put(key_proxy_type, key_browser); } else { proxySettingMap.put(key_direct, networkBean); proxySettingMap.put(key_proxy_type, key_direct); } userCurrShowNetworkBean = networkBean; isConfirm = true; setVisible(false); NetworkProxyTool.saveNetworkProxyConfig(proxySettingMap); }
System Properties
Java System Properties網絡設置有哪些key
http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html
http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html
Java System Properties優先級說明
host和port比set優先級高,也就是說set可以不用去設置。
如存在http.proxyHost和http.proxyPort時,proxySet設為false無效,系統仍然可以使用http代理。
存在socksProxyHost和socksProxyPort時,socksProxySet設為false無效,系統仍然可以使用socks代理。
http比socks優先級高,即存在http.proxyHost和http.proxyPort時,socksProxySet、socksProxyHost、socksProxyPort都會失效,系統會使用http代理。
使用java.net.Proxy
如果只是單個的連接需要使用代理,那么可以采用Proxy類進行代理連接。
說明:
1.Socket只能使用socks代理不能使用http代理。
2.Socket使用使用Authenticator來進行驗證。
3.URLConnection 使用RequestProperty是只對當前連接有效,不會緩存,Authenticator是全局性的,對所有網絡請求都有效,會緩存,但RequestProperty的優先級比Authenticator高。
4.只有RequestProperty需要64位編碼,Authenticator不需要。
5.需要清除驗證緩存的,不使用驗證緩存上面已經有代碼了,這里就不重復寫了。
6.其實,建議采用全局性的代理連接,不管是URLConnection 還是Socket 都簡單方便、統一。
代碼:
NetworkBean httpBean = new NetworkBean(Proxy.Type.HTTP, "192.168.77.5", "8888", "tzc", "123", null); Proxy httpProxy = new Proxy(httpBean.getType(), new InetSocketAddress(httpBean.getAddress(), Integer.parseInt(httpBean.getPort()))); NetworkBean socksBean = new NetworkBean(Proxy.Type.SOCKS, "192.168.77.5", "9999", "tzc", "123", "ttt"); Proxy socksProxy = new Proxy(socksBean.getType(), new InetSocketAddress(socksBean.getAddress(), Integer.parseInt(socksBean.getPort()))); URLConnection httpProxyConnection = new URL("http://www.baidu.com/").openConnection(httpProxy); //或者Authenticator.setDefault(new BairuiAuthenticator(networkBean.getDomainAndUsername(), networkBean.getPassword())); httpProxyConnection.setRequestProperty("Proxy-Authorization", "Basic "+httpBean.getDomainAndUsernameAndPassword64()); URLConnection socksProxyConnection = new URL("http://www.baidu.com/").openConnection(socksProxy); //或者Authenticator.setDefault(new BairuiAuthenticator(networkBean.getDomainAndUsername(), networkBean.getPassword())); socksProxyConnection.setRequestProperty("Proxy-Authorization", "Basic "+httpBean.getDomainAndUsernameAndPassword64()); Socket socket = new Socket(socksProxy); Authenticator.setDefault(new BairuiAuthenticator(networkBean.getDomainAndUsername(), networkBean.getPassword())); socket.connect(new InetSocketAddress("www.baidu.com", 5555));
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。