4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
import java.io.InputStream; |
import java.io.InputStream; |
6 |
import java.io.OutputStream; |
import java.io.OutputStream; |
7 |
|
import java.io.UnsupportedEncodingException; |
8 |
import java.net.HttpURLConnection; |
import java.net.HttpURLConnection; |
9 |
import java.net.URL; |
import java.net.URL; |
10 |
import java.net.URLConnection; |
import java.net.URLConnection; |
16 |
|
|
17 |
public class HttpUtil { |
public class HttpUtil { |
18 |
|
|
19 |
|
static public Map<String,String> decodeUri(String uri) { |
20 |
|
if (uri.indexOf('?') != -1) { |
21 |
|
String parts[] = uri.split("\\?"); |
22 |
|
return decodeParams( parts[1] ); |
23 |
|
} else { |
24 |
|
return new HashMap<String,String>(); |
25 |
|
} |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
} |
30 |
|
|
31 |
static public Map<String,String> decodeParams(String str) { |
static public Map<String,String> decodeParams(String str) { |
32 |
Map<String,String> res = new HashMap<String,String>(); |
Map<String,String> res = new HashMap<String,String>(); |
33 |
|
|
39 |
String key = pair[0]; |
String key = pair[0]; |
40 |
|
|
41 |
String val = null; |
String val = null; |
42 |
if (pair.length == 2) { |
if (pair.length == 2) { |
43 |
val = URLDecoder.decode(pair[1]); |
|
44 |
|
|
45 |
|
try { |
46 |
|
val = URLDecoder.decode(pair[1], "ISO-8859-1"); |
47 |
|
} catch (UnsupportedEncodingException e) { |
48 |
|
val = pair[1]; // if decode fails try with the raw string |
49 |
|
} |
50 |
|
|
51 |
} |
} |
52 |
|
|
53 |
res.put(key, val); |
res.put(key, val); |
98 |
} |
} |
99 |
} |
} |
100 |
|
|
101 |
|
public static String getContentString(String uri, int timeout) throws IOException { |
102 |
|
return getContentString(uri, timeout, "UTF-8"); |
103 |
|
} |
104 |
|
|
105 |
|
public static String getContentString(String uri, int timeout, String encoding) throws IOException { |
106 |
|
byte[] buf = getContent(uri, timeout); |
107 |
|
return new String(buf, encoding); |
108 |
|
} |
109 |
|
|
110 |
public static byte[] getContent(String uri, int timeout) throws IOException { |
public static byte[] getContent(String uri, int timeout) throws IOException { |
111 |
URL url = new URL(uri); |
URL url = new URL(uri); |