문제

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?

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top