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