示例#1
0
  /**
   * Run the first test.
   *
   * @param string a part of URL to connect to.
   * @return ManagerClient object
   * @throws IOException for any failures.
   */
  public ManagerClient(String string) throws Exception {
    URL = "http://" + string + "/mod_cluster_manager/";
    GetMethod gm = null;
    HttpMethodBase bm = null;
    if (httpClient == null) {
      httpClient = new HttpClient();
      gm = new GetMethod(URL);
      bm = gm;
    }

    System.out.println("Connecting to " + URL);

    Integer connectionTimeout = 40000;
    bm.getParams().setParameter("http.socket.timeout", connectionTimeout);
    bm.getParams().setParameter("http.connection.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.socket.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout);

    try {
      httpResponseCode = httpClient.executeMethod(gm);

      if (httpResponseCode == 200) {
        // Read the nonce.
        String result = gm.getResponseBodyAsString();
        String[] records = result.split("\n");
        for (int i = 0; i < records.length; i++) {
          int j = records[i].indexOf("?nonce=");
          if (j < 0) continue;
          j = j + 7;
          String nnonce = records[i].substring(j);
          int k = nnonce.indexOf('&');
          if (k > 0) {
            nonce = nnonce.substring(0, k);
            break;
          }
        }
      } else {
        System.out.println("response: " + httpResponseCode);
        System.out.println("response: " + bm.getStatusLine());
        throw (new Exception("Reponse notok"));
      }
      // System.out.println("response:\n" + bm.getResponseBodyAsString(len));
    } catch (HttpException e) {
      System.out.println("error: " + e);
      throw (e);
    }
    bm.releaseConnection();
  }
 /**
  * Requests the received method with the received timeout (milliseconds).
  *
  * <p>Executes the method through the inherited HttpClient.executedMethod(method).
  *
  * <p>Sets the socket and connection timeouts only for the method received.
  *
  * <p>The timeouts are both in milliseconds; 0 means 'infinite'; < 0 means 'do not change the
  * default'
  *
  * @param method HTTP method request.
  * @param readTimeout Timeout to set for data reception
  * @param conntionTimout Timeout to set for connection establishment
  */
 public int executeMethod(HttpMethodBase method, int readTimeout, int connectionTimeout)
     throws HttpException, IOException {
   int oldSoTimeout = getParams().getSoTimeout();
   int oldConnectionTimeout = getHttpConnectionManager().getParams().getConnectionTimeout();
   try {
     if (readTimeout >= 0) {
       method.getParams().setSoTimeout(readTimeout); // this should be enough...
       getParams().setSoTimeout(readTimeout); // ... but HTTPS needs this
     }
     if (connectionTimeout >= 0) {
       getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
     }
     return executeMethod(method);
   } finally {
     getParams().setSoTimeout(oldSoTimeout);
     getHttpConnectionManager().getParams().setConnectionTimeout(oldConnectionTimeout);
   }
 }