Here you can find the source of readRaw(URL url)
Parameter | Description |
---|---|
url | the URL to read from |
Parameter | Description |
---|---|
IOException | if a problem occurred when reading the data |
public static String readRaw(URL url) throws IOException
//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(); } }