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