Here you can find the source of waitForResponseCode(int code, String name, String host, int port)
public static HttpURLConnection waitForResponseCode(int code, String name, String host, int port) throws IOException
//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; } }