Here you can find the source of readUrl(URL url)
Parameter | Description |
---|---|
url | Url to read |
Parameter | Description |
---|---|
IOException | If an error occurs while reading the connexion |
public static String readUrl(URL url) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class Main { /**/*w ww .ja v a 2 s . c om*/ * Read the contents of a URL as text * @param url Url to read * @return Contents of the URL * @throws IOException If an error occurs while reading the connexion */ public static String readUrl(URL url) throws IOException { StringBuilder sb = new StringBuilder(); //ByteArrayOutputStream baos = new ByteArrayOutputStream(); URLConnection con = url.openConnection(); con.setConnectTimeout(1000); con.setReadTimeout(2000); InputStream in = con.getInputStream(); String enc = con.getHeaderField("Content-Type"); enc = enc.substring(enc.indexOf("charset=") > 0 ? enc.indexOf("charset=") + 8 : enc.length()); if (enc.length() < 3) enc = "UTF-8"; BufferedReader br = new BufferedReader(new InputStreamReader(in, enc)); String line; while ((line = br.readLine()) != null) sb.append(line).append('\n'); br.close(); return sb.toString(); } }