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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20