2

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?

4

1 回答 1

4

在我看来,为了实现这一点,您必须使用以下代码检查您的代理服务器是否处于活动状态。

如果布尔变量 conectionStatus 为真,则您的服务器处于活动状态。

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;
}
于 2013-06-16T16:05:38.603 回答