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

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

Description

get Response

License

Open Source License

Declaration

public static String getResponse(String name, String host, int port)
            throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************* 
 * Copyright (c) 2007 Red Hat, Inc. //from ww  w.j  a  v a2s. c om
 * 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.BufferedInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import java.net.HttpURLConnection;

import java.net.URL;

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

    public static String getResponse(String name, String host, int port)
            throws IOException {
        URL url = new URL("http://" + host + ":" + port + "/" + name);
        HttpURLConnection connection = connect(url);
        return toString(new BufferedInputStream(connection.getInputStream()));
    }

    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;
    }

    public static String toString(InputStream in) throws IOException {
        StringWriter writer = new StringWriter();
        for (int data = -1; ((data = in.read()) != -1);) {
            writer.write(data);
        }
        return writer.toString();
    }
}

Related

  1. getRandomOpenPort()
  2. getRandomPort()
  3. getRandomPort()
  4. getRandomPort()
  5. getRandomPorts(int n)
  6. getServerPort()
  7. getSupportedConnectionTypes(Map map)
  8. getSupportedEntityTypes()
  9. getUnusedPort()