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 |
|
|
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 |
torben |
1351 |
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 |
torben |
1349 |
} |
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 |
torben |
1356 |
public static String getContentString(String uri, int timeout) throws IOException { |
88 |
|
|
return getContentString(uri, timeout, "UTF-8"); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
public static String getContentString(String uri, int timeout, String encoding) throws IOException { |
92 |
|
|
byte[] buf = getContent(uri, timeout); |
93 |
|
|
return new String(buf, encoding); |
94 |
|
|
} |
95 |
torben |
1349 |
|
96 |
|
|
public static byte[] getContent(String uri, int timeout) throws IOException { |
97 |
|
|
URL url = new URL(uri); |
98 |
|
|
URLConnection connection = url.openConnection(); |
99 |
|
|
|
100 |
|
|
connection.setConnectTimeout(timeout); |
101 |
|
|
InputStream is = connection.getInputStream(); |
102 |
|
|
|
103 |
|
|
return readInputStream(is); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
107 |
|
|
URL url = new URL(uri); |
108 |
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
109 |
|
|
connection.setDoOutput(true); |
110 |
|
|
connection.setRequestMethod("POST"); |
111 |
|
|
connection.setConnectTimeout(timeout); |
112 |
|
|
|
113 |
|
|
OutputStream os = connection.getOutputStream(); |
114 |
|
|
os.write( data.getBytes() ); |
115 |
|
|
|
116 |
|
|
InputStream is = connection.getInputStream(); |
117 |
|
|
|
118 |
|
|
return readInputStream(is); |
119 |
|
|
|
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
static byte[] readInputStream(InputStream is) throws IOException{ |
123 |
|
|
byte buffer[] = new byte[1024]; |
124 |
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
125 |
|
|
try { |
126 |
|
|
int count; |
127 |
|
|
while ( (count = is.read(buffer)) != -1) { |
128 |
|
|
baos.write(buffer, 0, count); |
129 |
|
|
} |
130 |
|
|
} finally { |
131 |
|
|
try { |
132 |
|
|
is.close(); |
133 |
|
|
baos.close(); |
134 |
|
|
} catch (Exception e) {} //never mind if close throws an exception |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
return baos.toByteArray(); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
} |