分享java中設置代理的兩種方式
1 前言
有時候我們的程序中要提供可以使用代理訪問網絡,代理的方式包括http、https、ftp、socks代理。比如在IE瀏覽器設置代理。

那我們在我們的java程序中使用代理呢,有如下兩種方式。直接上代碼.
2 采用設置系統(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();
// 設置http訪問要使用的代理服務器的地址
prop.setProperty("http.proxyHost", "183.45.78.31");
// 設置http訪問要使用的代理服務器的端口
prop.setProperty("http.proxyPort", "8080");
// 設置不需要通過代理服務器訪問的主機,可以使用*通配符,多個地址用|分隔
prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 設置安全訪問使用的代理服務器地址與端口
// 它沒有https.nonProxyHosts屬性,它按照http.nonProxyHosts 中設置的規(guī)則訪問
prop.setProperty("https.proxyHost", "183.45.78.31");
prop.setProperty("https.proxyPort", "443");
// 使用ftp代理服務器的主機、端口以及不需要使用ftp代理服務器的主機
prop.setProperty("ftp.proxyHost", "183.45.78.31");
prop.setProperty("ftp.proxyPort", "21");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
// socks代理服務器的地址與端口
prop.setProperty("socksProxyHost", "183.45.78.31");
prop.setProperty("socksProxyPort", "1080");
// 設置登陸到代理服務器的用戶名和密碼
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)建代理服務器
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"));// 設置代理的用戶和密碼
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);// 設置代理訪問
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 總結
OK,就這么的簡單,搞定,用第一種方式是一種全局的代理,用第種方式可以針對具體的哪一個使用代理。知道了這些我們就可以做我們想做的事情了哦!
相關文章
Junit 5中@ParameterizedTest與@EnumSource結合使用
今天小編就為大家分享一篇關于Junit 5中@ParameterizedTest與@EnumSource結合使用,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
SpringBoot+jpa配置如何根據實體類自動創(chuàng)建表
這篇文章主要介紹了SpringBoot+jpa配置如何根據實體類自動創(chuàng)建表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot整合EasyExcel實現(xiàn)大規(guī)模數據的并行導出與壓縮下載
在 Spring Boot 應用中,整合 EasyExcel 實現(xiàn)并行導出數據并進行 Zip 壓縮下載可以極大地提高數據處理效率和用戶體驗,文中通過代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2024-10-10
java實現(xiàn)gif動畫效果(java顯示動態(tài)圖片)
這篇文章主要介紹了java實現(xiàn)gif動畫效果示例(java顯示動態(tài)圖片),需要的朋友可以參考下2014-04-04
Java中的BufferedInputStream與BufferedOutputStream使用示例
BufferedInputStream和BufferedOutputStream分別繼承于FilterInputStream和FilterOutputStream,代表著緩沖區(qū)的輸入輸出,這里我們就來看一下Java中的BufferedInputStream與BufferedOutputStream使用示例:2016-06-06

