1 |
package dk.thoerup.genericjavautils; |
2 |
|
3 |
import java.io.ByteArrayOutputStream; |
4 |
import java.io.IOException; |
5 |
import java.io.InputStream; |
6 |
import java.io.OutputStream; |
7 |
import java.io.UnsupportedEncodingException; |
8 |
import java.net.HttpURLConnection; |
9 |
import java.net.URL; |
10 |
import java.net.URLConnection; |
11 |
import java.net.URLDecoder; |
12 |
import java.net.URLEncoder; |
13 |
import java.util.HashMap; |
14 |
import java.util.Map; |
15 |
|
16 |
|
17 |
public class HttpUtil { |
18 |
|
19 |
static public Map<String,String> decodeParams(String str) { |
20 |
Map<String,String> res = new HashMap<String,String>(); |
21 |
|
22 |
|
23 |
String parts[] = str.split("&"); |
24 |
|
25 |
for (String part : parts) { |
26 |
String pair[] = part.split("="); |
27 |
String key = pair[0]; |
28 |
|
29 |
String val = null; |
30 |
if (pair.length == 2) { |
31 |
try { |
32 |
val = URLDecoder.decode(pair[1], "UTF-8"); |
33 |
} catch (UnsupportedEncodingException e) { |
34 |
val = pair[1]; // if decode fails try with the raw string |
35 |
} |
36 |
|
37 |
} |
38 |
|
39 |
res.put(key, val); |
40 |
} |
41 |
|
42 |
return res; |
43 |
} |
44 |
|
45 |
static public String getLastPart(String url) { |
46 |
String parts[] = url.split("/"); |
47 |
String lastPart = parts[ parts.length - 1]; |
48 |
|
49 |
return lastPart; |
50 |
} |
51 |
|
52 |
static public String encodeParams(Map<String,String> params) { |
53 |
StringBuilder sb = new StringBuilder(); |
54 |
|
55 |
boolean isFirst = true; |
56 |
for (String key : params.keySet()) { |
57 |
if (isFirst) { |
58 |
isFirst = false; |
59 |
} else { |
60 |
sb.append("&"); |
61 |
} |
62 |
|
63 |
String val = params.get(key); |
64 |
sb.append(key); |
65 |
sb.append("="); |
66 |
sb.append( encode(val) ); |
67 |
|
68 |
|
69 |
} |
70 |
|
71 |
|
72 |
return sb.toString(); |
73 |
|
74 |
} |
75 |
|
76 |
static String encode(String url) { |
77 |
if (url == null) |
78 |
return ""; |
79 |
|
80 |
try { |
81 |
return URLEncoder.encode(url, "UTF-8"); |
82 |
} catch (Exception e) { |
83 |
return url; |
84 |
} |
85 |
} |
86 |
|
87 |
|
88 |
public static byte[] getContent(String uri, int timeout) throws IOException { |
89 |
URL url = new URL(uri); |
90 |
URLConnection connection = url.openConnection(); |
91 |
|
92 |
connection.setConnectTimeout(timeout); |
93 |
InputStream is = connection.getInputStream(); |
94 |
|
95 |
return readInputStream(is); |
96 |
} |
97 |
|
98 |
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
99 |
URL url = new URL(uri); |
100 |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
101 |
connection.setDoOutput(true); |
102 |
connection.setRequestMethod("POST"); |
103 |
connection.setConnectTimeout(timeout); |
104 |
|
105 |
OutputStream os = connection.getOutputStream(); |
106 |
os.write( data.getBytes() ); |
107 |
|
108 |
InputStream is = connection.getInputStream(); |
109 |
|
110 |
return readInputStream(is); |
111 |
|
112 |
} |
113 |
|
114 |
static byte[] readInputStream(InputStream is) throws IOException{ |
115 |
byte buffer[] = new byte[1024]; |
116 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
117 |
try { |
118 |
int count; |
119 |
while ( (count = is.read(buffer)) != -1) { |
120 |
baos.write(buffer, 0, count); |
121 |
} |
122 |
} finally { |
123 |
try { |
124 |
is.close(); |
125 |
baos.close(); |
126 |
} catch (Exception e) {} //never mind if close throws an exception |
127 |
} |
128 |
|
129 |
return baos.toByteArray(); |
130 |
} |
131 |
|
132 |
} |