分享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)文章
Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用
今天小編就為大家分享一篇關(guān)于Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
SpringBoot+jpa配置如何根據(jù)實(shí)體類自動(dòng)創(chuàng)建表
這篇文章主要介紹了SpringBoot+jpa配置如何根據(jù)實(shí)體類自動(dòng)創(chuàng)建表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot整合EasyExcel實(shí)現(xiàn)大規(guī)模數(shù)據(jù)的并行導(dǎo)出與壓縮下載
在 Spring Boot 應(yīng)用中,整合 EasyExcel 實(shí)現(xiàn)并行導(dǎo)出數(shù)據(jù)并進(jìn)行 Zip 壓縮下載可以極大地提高數(shù)據(jù)處理效率和用戶體驗(yàn),文中通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-10-10
聊聊DecimalFormat的用法及各符號(hào)的意義
這篇文章主要介紹了DecimalFormat的用法及各符號(hào)的意義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
java實(shí)現(xiàn)gif動(dòng)畫(huà)效果(java顯示動(dòng)態(tài)圖片)
這篇文章主要介紹了java實(shí)現(xiàn)gif動(dòng)畫(huà)效果示例(java顯示動(dòng)態(tài)圖片),需要的朋友可以參考下2014-04-04
Java中的BufferedInputStream與BufferedOutputStream使用示例
BufferedInputStream和BufferedOutputStream分別繼承于FilterInputStream和FilterOutputStream,代表著緩沖區(qū)的輸入輸出,這里我們就來(lái)看一下Java中的BufferedInputStream與BufferedOutputStream使用示例:2016-06-06

