/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/util/HttpUtil.java
ViewVC logotype

Contents of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/util/HttpUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20