/[projects]/miscJava/GenericJavaUtils/src/dk/thoerup/genericjavautils/HttpUtil.java
ViewVC logotype

Annotation of /miscJava/GenericJavaUtils/src/dk/thoerup/genericjavautils/HttpUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20