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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1349 by torben, Wed Apr 20 18:45:54 2011 UTC revision 1413 by torben, Mon May 2 15:13:56 2011 UTC
# Line 4  import java.io.ByteArrayOutputStream; Line 4  import java.io.ByteArrayOutputStream;
4  import java.io.IOException;  import java.io.IOException;
5  import java.io.InputStream;  import java.io.InputStream;
6  import java.io.OutputStream;  import java.io.OutputStream;
7    import java.io.UnsupportedEncodingException;
8  import java.net.HttpURLConnection;  import java.net.HttpURLConnection;
9  import java.net.URL;  import java.net.URL;
10  import java.net.URLConnection;  import java.net.URLConnection;
# Line 15  import java.util.Map; Line 16  import java.util.Map;
16    
17  public class HttpUtil {  public class HttpUtil {
18                    
19            static public Map<String,String> decodeUri(String uri) {
20                    String parts[] = uri.split("\\?");
21                    return decodeParams( parts[1] );
22            }
23            
24          static public Map<String,String> decodeParams(String str) {          static public Map<String,String> decodeParams(String str) {
25                  Map<String,String> res = new HashMap<String,String>();                  Map<String,String> res = new HashMap<String,String>();
26                                    
# Line 26  public class HttpUtil { Line 32  public class HttpUtil {
32                          String key = pair[0];                          String key = pair[0];
33                                                    
34                          String val = null;                          String val = null;
35                          if (pair.length == 2) {                                          if (pair.length == 2) {
36                                  val = URLDecoder.decode(pair[1]);                                  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                          }                          }
43                                                    
44                          res.put(key, val);                          res.put(key, val);
# Line 78  public class HttpUtil { Line 89  public class HttpUtil {
89                  }                  }
90          }          }
91                    
92            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    
101          public static byte[] getContent(String uri, int timeout) throws IOException {          public static byte[] getContent(String uri, int timeout) throws IOException {
102                  URL url = new URL(uri);                  URL url = new URL(uri);

Legend:
Removed from v.1349  
changed lines
  Added in v.1413

  ViewVC Help
Powered by ViewVC 1.1.20