Java HTTP Port Find waitForResponseCode(int code, String name, String host, int port)

Here you can find the source of waitForResponseCode(int code, String name, String host, int port)

Description

wait For Response Code

License

Open Source License

Declaration

public static HttpURLConnection waitForResponseCode(int code,
            String name, String host, int port) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************* 
 * Copyright (c) 2007 Red Hat, Inc. /*  w ww. j  av a 2s  . c  o  m*/
 * Distributed under license by Red Hat, Inc. All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 * 
 * Contributors: 
 * Red Hat, Inc. - initial API and implementation 
 ******************************************************************************/

import java.io.FileNotFoundException;
import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    private static final int RESPONSE_TIMEOUT = 10 * 1024;
    private static final long WAIT_TIMEOUT = 10 * 1024;

    public static HttpURLConnection waitForResponseCode(int code,
            String name, String host, int port) throws IOException {
        URL url = new URL("http://" + host + ":" + port + "/" + name);
        long until = System.currentTimeMillis() + WAIT_TIMEOUT;
        while (System.currentTimeMillis() < until) {
            HttpURLConnection connection = connect(url);
            try {
                if (connection.getResponseCode() == code) {
                    return connection;
                }
            } catch (FileNotFoundException e) {
                if (code == 404) {
                    return connection;
                }
                throw e;
            }
        }
        throw new RuntimeException("wait on url " + url
                + " for response code " + code + " timed out.");
    }

    private static HttpURLConnection connect(URL url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setAllowUserInteraction(false);
        connection.setConnectTimeout(RESPONSE_TIMEOUT);
        connection.setInstanceFollowRedirects(true);
        connection.setDoOutput(false);
        return connection;
    }
}

Related

  1. ServerListening(String host, int port)
  2. serverListening(String host, int port)
  3. serverListening(String hostName, int port)
  4. waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin)
  5. waitForPort(String host, int port)
  6. waitForRespose(String name, String host, int port)
  7. waitForServerDown(String host, int port, long timeout)
  8. waitForService(int port)
  9. writeContactFile(int port, String appName)