Java URL Read readUrlContent(URLConnection connection)

Here you can find the source of readUrlContent(URLConnection connection)

Description

Whole HTTP response as String from given URLConnection

License

Open Source License

Parameter

Parameter Description
connection a parameter

Return

whole HTTP response as String

Declaration

public static String readUrlContent(URLConnection connection) 

Method Source Code

//package com.java2s;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import java.net.URLConnection;

public class Main {
    /**//from  w w  w  .  j av  a  2s.com
     * Whole HTTP response as String from given URLConnection
     *
     * @param connection
     * @return whole HTTP response as String
     */
    public static String readUrlContent(URLConnection connection) {
        StringBuilder result = new StringBuilder();
        try {
            Reader reader = new InputStreamReader(
                    connection.getInputStream());
            char[] buffer = new char[50];
            int nrOfChars;
            while ((nrOfChars = reader.read(buffer)) != -1) {
                result.append(buffer, 0, nrOfChars);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result.toString();
    }
}

Related

  1. readUrl(String urlString)
  2. readURL(URL url)
  3. readUrl(URL url)
  4. readUrl(URL url, String[]... headers)
  5. readURL(URLConnection connection, String charset)
  6. readURLToString(String url)
  7. readUrlToString(String urlString)
  8. readVersion(URLConnection connection)
  9. retrieve(URL url)