package dk.thoerup.traininfo.util; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.Date; public class DownloadUtil { public static String getContent(String uri, int timeout, String encoding) throws Exception { char cbuffer[] = new char[256]; StringBuffer buffer = new StringBuffer(128000); Date start = new Date(); URL url = new URL(uri); URLConnection connection = url.openConnection(); connection.setConnectTimeout(20000); InputStream stream = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(stream, encoding); int count; while ( (count = reader.read(cbuffer)) != -1) { buffer.append(cbuffer, 0, count); Date now = new Date(); if ( (now.getTime()-start.getTime()) > timeout) throw new Exception("timeout"); } return buffer.toString(); } }