• November 9, 2022


HTTP & SOCKS Rotating & Static Proxies

  • 72 million IPs for all purposes
  • Worldwide locations
  • 3 day moneyback guarantee

Visit brightdata.com

ProxySelector (Java Platform SE 7 ) – Oracle Help Center

public abstract class ProxySelector
extends Object
Selects the proxy server to use, if any, when connecting to the
network resource referenced by a URL. A proxy selector is a
concrete sub-class of this class and is registered by invoking the
setDefault method. The
currently registered proxy selector can be retrieved by calling
getDefault method.
When a proxy selector is registered, for instance, a subclass
of URLConnection class should call the select
method for each URL request so that the proxy selector can decide
if a direct, or proxied connection should be used. The select method returns an iterator over a collection with
the preferred connection approach.
If a connection cannot be established to a proxy (PROXY or
SOCKS) servers then the caller should call the proxy selector’s
connectFailed method to notify the proxy
selector that the proxy server is unavailable.
The default proxy selector does enforce a
set of System Properties
related to proxy settings.
1. 5
Constructor Summary
Constructor and Description
Method Summary
Modifier and Type
Method and Description
abstract void
connectFailed(URI uri,
SocketAddress sa,
IOException ioe)
Called to indicate that a connection could not be established
to a proxy/socks server.
static ProxySelector
Gets the system-wide proxy selector.
abstract List
select(URI uri)
Selects all the applicable proxies based on the protocol to
access the resource with and a destination address to access
the resource at.
static void
setDefault(ProxySelector ps)
Sets (or unsets) the system-wide proxy selector.
Methods inherited from class
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
public ProxySelector()
Method Detail
public static ProxySelector getDefault()
Returns:the system-wide ProxySelector
SecurityException – If a security manager has been installed and it denies
See Also:setDefault(ProxySelector)
public static void setDefault(ProxySelector ps)
Note: non-standard protocol handlers may ignore this setting.
Parameters:ps – The HTTP proxy selector, or
null to unset the proxy selector.
See Also:getDefault()
public abstract List select(URI uri)
The format of the URI is defined as follow:
URI for connections
ftp URI for ftp connections
for tcp client sockets connections
Parameters:uri – The URI that a connection is required to
Returns:a List of Proxies. Each element in the
the List is of type
when no proxy is available, the list will
contain one element of type
that represents a direct connection.
IllegalArgumentException – if the argument is null
public abstract void connectFailed(URI uri,
to a proxy/socks server. An implementation of this method can
temporarily remove the proxies or reorder the sequence of
proxies returned by select(URI), using the address
and the IOException caught when trying to connect.
Parameters:uri – The URI that the proxy at sa failed to – The socket address of the proxy/SOCKS serverioe – The I/O exception thrown when the connect failed.
IllegalArgumentException – if either argument is null
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2020, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
ProxySelector: Different Proxy for Each URL - Stack Overflow

HTTP Rotating & Static Proxies

  • 200 thousand IPs
  • Locations: US, EU
  • Monthly price: from $39
  • 1 day moneyback guarantee

Visit stormproxies.com

ProxySelector: Different Proxy for Each URL – Stack Overflow

I’m trying to understand how the ProxySelector class works. My current code looks like this:
URI uri = new URI(“);
I understand that when calling (uri); this is suppose to return a list of proxies for the respective URI. But I don’t see how to set the proxy for each URI.
I know I could set the default proxy using the setDefault() method but as far as I understand this will set the system wide proxy, not the proxy for a specific URI.
I might be missing some basic point here but how do I set one proxy for url A (such as) and a different proxy for url B (such as) and then have the system automatically select the correct proxy each time it connects to the corresponding url?
Michal Foksa8, 9957 gold badges41 silver badges62 bronze badges
asked Nov 6 ’17 at 16:02
Override (URI uri) method where you implement custom logic to choose right proxy or list of proxies for the URI.
either set the new, custom, ProxySelector as system wide by calling tDefault(customProxySelector).
Any subclass of URLConnection will use ProxySelector, e. g. :
URLConnection conn = Connection();
or configure framework you are going to use to invoke remote URI, e. g Spring RestTemplate:
HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new MyProxySelector());
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory
= new HttpComponentsClientHttpRequestFactory(
(). setRoutePlanner(routePlanner)
restTemplate = new RestTemplate(clientHttpRequestFactory);
It is good practice to fallback to default ProxySelector in custom select(URI uri) in case the custom logic does not determine suitable proxy for the uri.
See my other answer for ProxySelector example.
Networking and proxies is well explained in Java Networking and Proxies (paragraph 4 ProxySelector) and ProxySelector Java docs.
answered Nov 7 ’17 at 19:52
Michal FoksaMichal Foksa8, 9957 gold badges41 silver badges62 bronze badges
Actually, that works pretty well without any reflection, by just using an enhanced ProxySelector, that supports whitelisting for specific urls and passes the rest through to a delegate (aka the default) ProxySelector.
public class DelegatingProxySelector extends ProxySelector {
private final Set allProxiedUri = new HashSet<>();
private String proxyHost;
private Integer proxyPort;
private final ProxySelector delegate;
public DelegatingProxySelector(
String proxyHost, Integer proxyPort, ProxySelector delegate) {
oxyHost = proxyHost;
oxyPort = proxyPort;
legate = delegate;}
public List select(final URI uri) {
if (ntains(uri)) {
final InetSocketAddress proxyAddress = InetSocketAddress. createUnresolved(proxyHost, proxyPort);
return ngletonList(new Proxy(, proxyAddress));}
return (uri);}
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) {
(“Unable to reach uri ” + uri + ” via proxy: ” + tMessage());}
public void addProxiedUri(URI uri) {
final DelegatingProxySelector delegatingProxySelector = new DelegatingProxySelector(“localhost”, 3128, tDefault());
answered Nov 13 ’20 at 9:27
Not the answer you’re looking for? Browse other questions tagged java proxy uri -proxy proxyselector or ask your own question.
java.net.ProxySelector.setDefault java code examples | Tabnine

java.net.ProxySelector.setDefault java code examples | Tabnine

Best Java code snippets using (Showing top 20 results out of 315)Common ways to obtain ProxySelectorprivate void myMethod () {}static void install(String hostToIgnore, int portToIgnore) {
ProxySelector defaultProxySelector = tDefault();
ProxySelector ignoreHostProxySelector = new IgnoreHostProxySelector(defaultProxySelector, hostToIgnore, portToIgnore);
@Test(enabled = false)
public void testUseProxySelector() throws IOException, ExecutionException, TimeoutException, InterruptedException {
ProxySelector originalProxySelector = tDefault();
tDefault(new ProxySelector() {
public List select(URI uri) {
if (tHost()(“127. 0. 1”)) {
ProxySearch proxySearch = new ProxySearch();
dStrategy(Strategy. OS_DEFAULT);
ProxySelector proxySelector = tProxySelector();
URI home = (“);
(“ProxySelector: ” + proxySelector);
(“URI: ” + home);
List proxyList = (home);
if (proxyList! = null &&! Empty()) {
for (Proxy proxy: proxyList) {
SocketAddress address = dress();
if (address instanceof InetSocketAddress) {
String host = ((InetSocketAddress) address). getHostName();
String port = String(((InetSocketAddress) address). getPort());
tProperty(“oxyHost”, host);
tProperty(“oxyPort”, port);}}}
public static void setDefault() {
public static void registerAsDefault() {
public void uninstall() {
if (seedProxySelector! = null) {
if (seedProxyAuthenticator! = null) {
public static void setupDefaultProxySelector(){
public void inject() {
ProxySelector proxySelector = tDefault();
if (proxySelector! = this) {
oldProxySelector = proxySelector;
public void restore() {
if (tDefault() == this) {
if(proxySelector! = null){
proxySelector = tDefault();}
private void setupProxy(final BundleContext context) {
final ServiceReference proxy;
proxy = tServiceReference(());
if (proxy! = null) {
tDefault(new EclipseProxySelector(
(IProxyService) tService(proxy)));
tDefault(new EclipseAuthenticator(
(IProxyService) tService(proxy)));}}
protected void serverStarted(final Integer port, final Integer securePort) {
tProperty(“proxySet”, “true”);
tProperty(“oxyHost”, “127. 1”);
tProperty(“”, “true”);
int socksPort = socksPort();
if (socksPort! = -1) {
tProperty(“oxyPort”, “” + socksPort);
(proxySelector());} else {
tProperty(“oxyPort”, String());}}
static void register() {
ProxySelector prev = tDefault();
if (prev == null) {
LOG. warning(“No default system ProxySelector was found thus NetBeans ProxySelector won’t delegate on it”);} else {
(, “Override the original ProxySelector: {0}”, prev);}
ProxySelector proxySelector = new ProxySelector() {
List proxies = new ArrayList();
return proxies;}
public void connectFailed(URI uri, SocketAddress address,
IOException failure) {}};
public Void run() {
ProxySelector selector = tDefault();
if(LoadedInClassLoader(selector)) {
(“Removing default ” + selector);}
return null;}});
if(isLoadedInWebApplication(selector)) {
warn(“Removing default ” + selector);}
public static void main(String[] args) throws Exception {
ProxySelector proxy = tDefault();
SoapUIRun srun = new SoapUIRun();
nTestCase(“CPIHS”, “CPIHS”);
new MainPage(). LoginCad();}
protected void triggerLeak() throws Exception {
tDefault(new MyProxySelector());}
WsdlProject wsdlProject = new WsdlProject(“”);
WebDriver driver = new FirefoxDriver();
public void afterTest() throws Exception {

Frequently Asked Questions about proxyselector

Leave a Reply

Your email address will not be published. Required fields are marked *