分享java中設(shè)置代理的兩種方式
1 前言
有時(shí)候我們的程序中要提供可以使用代理訪問(wèn)網(wǎng)絡(luò),代理的方式包括http、https、ftp、socks代理。比如在IE瀏覽器設(shè)置代理。
那我們?cè)谖覀兊膉ava程序中使用代理呢,有如下兩種方式。直接上代碼.
2 采用設(shè)置系統(tǒng)屬性
import java.net.Authenticator; import java.net.PasswordAuthentication; import java.util.Properties; public class ProxyDemo1 { public static void main(String[] args) { Properties prop = System.getProperties(); // 設(shè)置http訪問(wèn)要使用的代理服務(wù)器的地址 prop.setProperty("http.proxyHost", "183.45.78.31"); // 設(shè)置http訪問(wèn)要使用的代理服務(wù)器的端口 prop.setProperty("http.proxyPort", "8080"); // 設(shè)置不需要通過(guò)代理服務(wù)器訪問(wèn)的主機(jī),可以使用*通配符,多個(gè)地址用|分隔 prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*"); // 設(shè)置安全訪問(wèn)使用的代理服務(wù)器地址與端口 // 它沒(méi)有https.nonProxyHosts屬性,它按照http.nonProxyHosts 中設(shè)置的規(guī)則訪問(wèn) prop.setProperty("https.proxyHost", "183.45.78.31"); prop.setProperty("https.proxyPort", "443"); // 使用ftp代理服務(wù)器的主機(jī)、端口以及不需要使用ftp代理服務(wù)器的主機(jī) prop.setProperty("ftp.proxyHost", "183.45.78.31"); prop.setProperty("ftp.proxyPort", "21"); prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*"); // socks代理服務(wù)器的地址與端口 prop.setProperty("socksProxyHost", "183.45.78.31"); prop.setProperty("socksProxyPort", "1080"); // 設(shè)置登陸到代理服務(wù)器的用戶名和密碼 Authenticator.setDefault(new MyAuthenticator("userName", "Password")); } static class MyAuthenticator extends Authenticator { private String user = ""; private String password = ""; public MyAuthenticator(String user, String password) { this.user = user; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password.toCharArray()); } } }
3 使用Proxy
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.Proxy; import java.net.URL; public class ProxyDemo2 { public static void main(String[] args) throws Exception { URL url = new URL("http://www.3lai8.com"); // /創(chuàng)建代理服務(wù)器 InetSocketAddress addr = new InetSocketAddress("192.168.0.254", 8080); // Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理 Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理 Authenticator.setDefault(new MyAuthenticator("username", "password"));// 設(shè)置代理的用戶和密碼 HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);// 設(shè)置代理訪問(wèn) InputStreamReader in = new InputStreamReader(connection.getInputStream()); BufferedReader reader = new BufferedReader(in); while (true) { String s = reader.readLine(); if (s != null) { System.out.println(s); } } } static class MyAuthenticator extends Authenticator { private String user = ""; private String password = ""; public MyAuthenticator(String user, String password) { this.user = user; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password.toCharArray()); } } }
4 總結(jié)
OK,就這么的簡(jiǎn)單,搞定,用第一種方式是一種全局的代理,用第種方式可以針對(duì)具體的哪一個(gè)使用代理。知道了這些我們就可以做我們想做的事情了哦!
相關(guān)文章
Java從ftp服務(wù)器上傳與下載文件的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Java從ftp服務(wù)器上傳與下載文件的實(shí)現(xiàn)方法,最近項(xiàng)目中需要實(shí)現(xiàn)將文件先存放到ftp上,需要的時(shí)候再?gòu)膄tp上下載,做的過(guò)程中碰到了問(wèn)題,所以這里總結(jié)下,需要的朋友可以參考下2023-08-08JAXB命名空間_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了JAXB命名空間的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Mybatis分頁(yè)插件PageHelper的使用詳解
這篇文章主要介紹了Mybatis分頁(yè)插件PageHelper的相關(guān)資料,該插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫(kù)分頁(yè)需要的朋友可以參考下2016-12-12JavaWeb建立簡(jiǎn)單三層項(xiàng)目步驟圖解
這篇文章主要介紹了JavaWeb建立簡(jiǎn)單三層項(xiàng)目步驟圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能
這篇文章主要介紹了SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能,pringBoot分頁(yè)查詢的兩種寫(xiě)法,一種是手動(dòng)實(shí)現(xiàn),另一種是使用框架實(shí)現(xiàn),現(xiàn)在我將具體的實(shí)現(xiàn)流程分享一下,需要的朋友可以參考下2023-11-11