Search code examples
javahttphttpclientproxy-server

How to programmatically check that the proxy server is alive or not in Java


How to programmatically in Java check whether a proxy server is alive or works and can be used? I mean I can use this proxy in order to access external services via this proxy.

For instance, setting up an Authenticated Proxy with code like this:

httpclient.getCredentialsProvider().setCredentials(
        new AuthScope(proxyHostNameOrIp, 8282),   
        new UsernamePasswordCredentials(username, password));

what if 'username' and 'password' were entered by the user (and entered incorrectly)? Is there any mechanism to 'validate' that the proxy information is valid assuming it has all been entered by a user?


Solution

  • In my opinion in order to achieve this you have to check your proxy server is alive or not by using following piece of code.

    If boolean variable conectionStatus is true your server is alive.

    public boolean testConnection() {
      boolean connectionStatus=false;
    
      try {
          InetAddress addr=InetAddress.getByName("8.8.8.8");//here type proxy server ip      
    
    
          connectionStatus=addr.isReachable(1000); // 1 second time for response
    
      }                               
      catch (Exception e) {
          e.printStackTrace();
          System.out.println(e.toString());
      }
    
      return connectionStatus;
    }