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

  ViewVC Help
Powered by ViewVC 1.1.20