1 |
torben |
238 |
package dk.thoerup.traininfo.util;
|
2 |
|
|
|
3 |
|
|
import java.io.InputStream;
|
4 |
|
|
import java.io.InputStreamReader;
|
5 |
|
|
import java.net.URL;
|
6 |
|
|
import java.net.URLConnection;
|
7 |
|
|
import java.util.Date;
|
8 |
|
|
|
9 |
|
|
public class DownloadUtil {
|
10 |
|
|
|
11 |
|
|
public static String getContent(String uri, int timeout, String encoding) throws Exception {
|
12 |
|
|
char cbuffer[] = new char[256];
|
13 |
|
|
StringBuffer buffer = new StringBuffer(128000);
|
14 |
|
|
|
15 |
|
|
Date start = new Date();
|
16 |
|
|
URL url = new URL(uri);
|
17 |
|
|
URLConnection connection = url.openConnection();
|
18 |
|
|
connection.setConnectTimeout(20000);
|
19 |
|
|
InputStream stream = connection.getInputStream();
|
20 |
|
|
|
21 |
|
|
InputStreamReader reader = new InputStreamReader(stream, encoding);
|
22 |
|
|
int count;
|
23 |
|
|
while ( (count = reader.read(cbuffer)) != -1) {
|
24 |
|
|
buffer.append(cbuffer, 0, count);
|
25 |
|
|
|
26 |
|
|
Date now = new Date();
|
27 |
|
|
if ( (now.getTime()-start.getTime()) > timeout)
|
28 |
|
|
throw new Exception("timeout");
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
return buffer.toString();
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
}
|