Java URL Read readUrl(URL url, String[]... headers)

Here you can find the source of readUrl(URL url, String[]... headers)

Description

read Url

License

Open Source License

Declaration

public static String readUrl(URL url, String[]... headers)
            throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String readUrl(URL url, String[]... headers)
            throws IOException {
        BufferedReader reader = null;
        StringBuilder buffer = new StringBuilder();

        try {/*from w  w w.  j  a  v a2 s .  c  om*/
            URLConnection connection = url.openConnection();
            for (String[] header : headers)
                connection.addRequestProperty(header[0], header[1]);
            reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));

            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1)
                buffer.append(chars, 0, read);
        } finally {
            if (reader != null)
                reader.close();
        }
        return buffer.toString();
    }
}

Related

  1. readURL(String path)
  2. readUrl(String url_str)
  3. readUrl(String urlString)
  4. readURL(URL url)
  5. readUrl(URL url)
  6. readURL(URLConnection connection, String charset)
  7. readUrlContent(URLConnection connection)
  8. readURLToString(String url)
  9. readUrlToString(String urlString)