Java HTTP Response getResponse(URL url)

Here you can find the source of getResponse(URL url)

Description

Returns the content of the response body for the given url

License

Apache License

Parameter

Parameter Description
url a parameter

Exception

Parameter Description
IOException an exception

Return

the content of the response body

Declaration

public static String getResponse(URL url) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    private static final String URL = "URL";
    private static final String GET = "GET";

    /**//from  w  w  w  . j a va 2s.  co m
     * Returns the content of the response body for the given url
     * 
     * @param url
     * @return the content of the response body
     * @throws IOException
     */
    public static String getResponse(URL url) throws IOException {

        StringBuffer response = new StringBuffer();
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(GET);
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                response.append(sCurrentLine);
            }
        }
        return response.toString();
    }
}

Related

  1. getResourceLastModified(URLConnection connection)
  2. getResourceModifiedTime(URL url)
  3. getResponse(HttpURLConnection httpConn, StringBuilder responseContent, StringBuilder responseCode, HashMap headers)
  4. getResponse(URL request)
  5. getResponseAsString(HttpURLConnection conn)
  6. getResponseBody(HttpURLConnection conn)
  7. getResponseContent(HttpURLConnection connection)
  8. getResponseContent(HttpURLConnection httpConn)