ProxySelector 確定必須通過代理請求哪個資源作為結果返回列表<代理>
ProxySelector類的方法:
方法 | 說明 |
---|---|
connectFailed() | 建立連接失敗時調用該方法 |
getDefault() | 此方法用於檢索system-wideProxySelector |
select() | 該方法返回代理人訪問資源 |
setDefault() | 該方法用於設置或取消設置system-wide ProxySelector |
示例:邏輯示例代碼
Java
// Java Program to illustrate ProxySelector Class
// of java.net package
// only creating methods here
// Importing standard input output classes
import java.io.IOException;
// Importing classes from java.net package
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
// Importing List and ArrayList as utility classes from
// java.util package
import java.util.ArrayList;
import java.util.List;
// Class 1
// Helper class extending ProxySelector class
public class PrivateDataProxy extends ProxySelector {
// According to API we need to return List<Proxy>
// even if we return only one element, so
// Creating List class object of Proxy type
private final List<Proxy> noProxy = new ArrayList<>();
private final List<Proxy> proxies = new ArrayList<>();
// Constructor of this class
public PrivateDataProxy()
{
// If no proxy required to access resource
// use Proxy.NO_PROXY
noProxy.add(Proxy.NO_PROXY);
// Creating InetSocketAddress, and
// secure.connection.com doesn't exist 443 is an
// https port
InetSocketAddress inetSocketAddress
= new InetSocketAddress("secure.connection.com",
443);
// Now creating http proxy
Proxy proxy
= new Proxy(Proxy.Type.HTTP, inetSocketAddress);
// Finally adding proxy into proxy list
proxies.add(proxy);
}
// Method 1 of this class
//@Override
public List<Proxy> select(URI uri)
{
if (uri.getPath().startsWith("/confidential")) {
// If URI path starts with '/confidential' then
// use proxy server
return proxies;
}
// If url don't start with '/confidential' then
// no need in proxy
return noProxy;
}
// Method 2 of this class
// @Override
public void connectFailed(URI arg0, SocketAddress arg1,
IOException arg2)
{
// Properly handle connection failing
}
}
執行:使用自定義ProxySelector
Java
// Java Program to illustrate ProxySelector Class
// of java.net package
// Using custom ProxySelector
// Importing required classes from respective packages
import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
// Main class
public class ProxySelectorDemo {
// Main driver method
public static void main(String[] args)
throws URISyntaxException, IOException
{
// Passing the string uri
PrivateDataProxy privateDataProxy
= new PrivateDataProxy();
// The setting the system-wide proxy selector
ProxySelector.setDefault(privateDataProxy);
// Print the default value
// using getDefault() method
System.out.println("Default value: "
+ ProxySelector.getDefault());
// Display message only
System.out.println(
"Getting proxy for /confidential");
// Passing the string URL
String confidentialUrl
= "https://www.download.com/confidential";
// Now, calling the constructor of the URL class
URL confidential = new URL(confidentialUrl);
// Requiring an proxy for url
List<Proxy> confidentialProxies
= privateDataProxy.select(confidential.toURI());
// Show the proxy that was selected
System.out.println("Proxy to use : "
+ confidentialProxies.get(0));
// Display message only
System.out.println(
"Getting proxy for /non-confidential");
// passing the string URL
// Custom URL as input
String nonConfidentialURL
= "https://www.download.com/non-confidential";
// Now, calling the constructor of the URL class
URL nonConfidential = new URL(nonConfidentialURL);
// Requiring an proxy for URL
List<Proxy> nonConfidentialProxies
= privateDataProxy.select(
nonConfidential.toURI());
// Display the proxy that was selected
System.out.println("Proxy to use : "
+ nonConfidentialProxies.get(0));
}
}
輸出:
Default value: entity.PrivateDataProxy@5cad8086 Getting proxy for /confidential Proxy to use : HTTP @ secure.connection.com:443 Getting proxy for /non-confidential Proxy to use : DIRECT
相關用法
- Java java.net.Proxy用法及代碼示例
- Java java.net.ProtocolFamily用法及代碼示例
- Java java.net.PasswordAuthentication用法及代碼示例
- Java java.net.SocketException用法及代碼示例
- Java java.net.SocketOption用法及代碼示例
- Java java.net.CookiePolicy用法及代碼示例
- Java java.net.SecureCacheResponse用法及代碼示例
- Java java.net.CacheResponse用法及代碼示例
- Java java.net.SocketImplFactory用法及代碼示例
- Java java.net.ResponseCache用法及代碼示例
- Java java.net.URLPermission用法及代碼示例
- Java java.net.NetPermission用法及代碼示例
- Java java.net.CacheRequest用法及代碼示例
- Java java.net.FileNameMap用法及代碼示例
- Java java.net.CookieStore用法及代碼示例
- Java java.net.CookieHandler用法及代碼示例
- Java java.net.CookieManager用法及代碼示例
- Java java.net.BindException用法及代碼示例
- Java java.net.URLConnection用法及代碼示例
- Java java.net.Socket用法及代碼示例
- Java java.net.ServerSocket用法及代碼示例
- Java java.net.InetAddress用法及代碼示例
- Java java.nio.ByteBuffer用法及代碼示例
- Java java.nio.IntBuffer用法及代碼示例
- Java java.nio.file.FileStore用法及代碼示例
注:本文由純淨天空篩選整理自alijakparovkz大神的英文原創作品 java.net.ProxySelector Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。