Java HTTP Read readRaw(URL url)

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

Description

Convenience method for reading a JSON string from a URL

License

Open Source License

Parameter

Parameter Description
url the URL to read from

Exception

Parameter Description
IOException if a problem occurred when reading the data

Return

the data fetched from the URL

Declaration

public static String readRaw(URL url) throws IOException 

Method Source Code


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

import java.io.BufferedReader;

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

public class Main {
    /**/*www.j  a  va2 s  . c om*/
     * Convenience method for reading a JSON string from a URL
     * 
     * @param url
     *            the URL to read from
     * @return the data fetched from the URL
     * @throws IOException
     *             if a problem occurred when reading the data
     */
    public static String readRaw(URL url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(5000);
        connection.setConnectTimeout(5000);
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.addRequestProperty("Content-Type", "application/json");
        connection.addRequestProperty("Accept", "application/json");

        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder content = new StringBuilder();
        String line = "";
        while ((line = br.readLine()) != null) {
            content.append(line);
        }
        return content.toString();
    }
}

Related

  1. readError(HttpURLConnection conn)
  2. readGetEx(String url)
  3. readHTTPConnection(HttpURLConnection conn)
  4. readHttpConnection(HttpURLConnection h)
  5. readHttpFile(String fileUrl, String referer)