1 |
torben |
1349 |
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 |
torben |
1351 |
import java.io.UnsupportedEncodingException; |
8 |
torben |
1349 |
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 |
torben |
1413 |
static public Map<String,String> decodeUri(String uri) { |
20 |
torben |
1421 |
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 |
torben |
1413 |
} |
30 |
|
|
|
31 |
torben |
1349 |
static public Map<String,String> decodeParams(String str) { |
32 |
|
|
Map<String,String> res = new HashMap<String,String>(); |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
String parts[] = str.split("&"); |
36 |
|
|
|
37 |
|
|
for (String part : parts) { |
38 |
|
|
String pair[] = part.split("="); |
39 |
|
|
String key = pair[0]; |
40 |
|
|
|
41 |
|
|
String val = null; |
42 |
torben |
1351 |
if (pair.length == 2) { |
43 |
torben |
1430 |
|
44 |
|
|
|
45 |
torben |
1351 |
try { |
46 |
torben |
1430 |
val = URLDecoder.decode(pair[1], "ISO-8859-1"); |
47 |
torben |
1351 |
} catch (UnsupportedEncodingException e) { |
48 |
|
|
val = pair[1]; // if decode fails try with the raw string |
49 |
|
|
} |
50 |
|
|
|
51 |
torben |
1349 |
} |
52 |
|
|
|
53 |
|
|
res.put(key, val); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
return res; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
static public String getLastPart(String url) { |
60 |
|
|
String parts[] = url.split("/"); |
61 |
|
|
String lastPart = parts[ parts.length - 1]; |
62 |
|
|
|
63 |
|
|
return lastPart; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
static public String encodeParams(Map<String,String> params) { |
67 |
|
|
StringBuilder sb = new StringBuilder(); |
68 |
|
|
|
69 |
|
|
boolean isFirst = true; |
70 |
|
|
for (String key : params.keySet()) { |
71 |
|
|
if (isFirst) { |
72 |
|
|
isFirst = false; |
73 |
|
|
} else { |
74 |
|
|
sb.append("&"); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
String val = params.get(key); |
78 |
|
|
sb.append(key); |
79 |
|
|
sb.append("="); |
80 |
|
|
sb.append( encode(val) ); |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
return sb.toString(); |
87 |
|
|
|
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
static String encode(String url) { |
91 |
|
|
if (url == null) |
92 |
|
|
return ""; |
93 |
|
|
|
94 |
|
|
try { |
95 |
|
|
return URLEncoder.encode(url, "UTF-8"); |
96 |
|
|
} catch (Exception e) { |
97 |
|
|
return url; |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
torben |
1356 |
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 |
torben |
1349 |
|
110 |
|
|
public static byte[] getContent(String uri, int timeout) throws IOException { |
111 |
|
|
URL url = new URL(uri); |
112 |
|
|
URLConnection connection = url.openConnection(); |
113 |
|
|
|
114 |
|
|
connection.setConnectTimeout(timeout); |
115 |
|
|
InputStream is = connection.getInputStream(); |
116 |
|
|
|
117 |
|
|
return readInputStream(is); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
121 |
|
|
URL url = new URL(uri); |
122 |
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
123 |
|
|
connection.setDoOutput(true); |
124 |
|
|
connection.setRequestMethod("POST"); |
125 |
|
|
connection.setConnectTimeout(timeout); |
126 |
|
|
|
127 |
|
|
OutputStream os = connection.getOutputStream(); |
128 |
|
|
os.write( data.getBytes() ); |
129 |
|
|
|
130 |
|
|
InputStream is = connection.getInputStream(); |
131 |
|
|
|
132 |
|
|
return readInputStream(is); |
133 |
|
|
|
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
static byte[] readInputStream(InputStream is) throws IOException{ |
137 |
|
|
byte buffer[] = new byte[1024]; |
138 |
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
139 |
|
|
try { |
140 |
|
|
int count; |
141 |
|
|
while ( (count = is.read(buffer)) != -1) { |
142 |
|
|
baos.write(buffer, 0, count); |
143 |
|
|
} |
144 |
|
|
} finally { |
145 |
|
|
try { |
146 |
|
|
is.close(); |
147 |
|
|
baos.close(); |
148 |
|
|
} catch (Exception e) {} //never mind if close throws an exception |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
return baos.toByteArray(); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
} |