Java HTTP Get readURL(URL url)

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

Description

read URL

License

Open Source License

Declaration

private static byte[] readURL(URL url) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    private static final String UTF_8 = "UTF-8";

    private static byte[] readURL(URL url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);//from w  ww.j  a  va2  s  .  com
        int code = connection.getResponseCode();
        if (code > HttpURLConnection.HTTP_OK) {
            handleError(connection);
        }
        InputStream stream = connection.getInputStream();

        if (stream == null) {
            throw new RuntimeException("Failed to get content from url " + url + " - no response stream");
        }
        byte[] content = read(stream);
        return content;
    }

    private static void handleError(HttpURLConnection connection) throws IOException {
        String msg = "Failed to upload media.";
        InputStream errorStream = connection.getErrorStream();
        if (errorStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(errorStream, UTF_8);
            BufferedReader bufferReader = new BufferedReader(inputStreamReader);
            try {
                StringBuilder builder = new StringBuilder();
                String outputString;
                while ((outputString = bufferReader.readLine()) != null) {
                    if (builder.length() != 0) {
                        builder.append("\n");
                    }
                    builder.append(outputString);
                }
                String response = builder.toString();
                msg += "Response: " + response;
            } finally {
                bufferReader.close();
            }
        }
        throw new RuntimeException(msg);
    }

    private static byte[] read(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int nBytes = 0;
            while ((nBytes = input.read(buffer)) > 0) {
                output.write(buffer, 0, nBytes);
            }
            byte[] result = output.toByteArray();
            return result;
        } finally {
            try {
                input.close();
            } catch (IOException e) {

            }
        }
    }
}

Related

  1. readURL(final String textURL)
  2. readUrl(HttpURLConnection conn)
  3. readUrl(String url, String token)
  4. readURL(String url, String type)
  5. readUrl(String urlAsString, int timeout)
  6. requestGetMethod(String url, String... params)