|
| 1 | +package io.ipfs.cluster.api; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | +import java.net.URL; |
| 8 | +import java.net.URLConnection; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +public class URLConnectionUtil { |
| 13 | + |
| 14 | + private static final String SERVLET_POST = "POST" ; |
| 15 | + private static final String SERVLET_GET = "GET" ; |
| 16 | + private static final String SERVLET_DELETE = "DELETE" ; |
| 17 | + private static final String SERVLET_PUT = "PUT" ; |
| 18 | + |
| 19 | + private static String prepareParam(Map<String,Object> paramMap){ |
| 20 | + StringBuffer sb = new StringBuffer(); |
| 21 | + if(paramMap.isEmpty()){ |
| 22 | + return "" ; |
| 23 | + }else{ |
| 24 | + for(String key: paramMap.keySet()){ |
| 25 | + String value = (String)paramMap.get(key); |
| 26 | + if(sb.length()<1){ |
| 27 | + sb.append(key).append("=").append(value); |
| 28 | + }else{ |
| 29 | + sb.append("&").append(key).append("=").append(value); |
| 30 | + } |
| 31 | + } |
| 32 | + return sb.toString(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static void doPost(String urlStr,Map<String,Object> paramMap ) throws Exception{ |
| 37 | + URL url = new URL(urlStr); |
| 38 | + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
| 39 | + conn.setRequestMethod(SERVLET_POST); |
| 40 | + String paramStr = prepareParam(paramMap); |
| 41 | + conn.setDoInput(true); |
| 42 | + conn.setDoOutput(true); |
| 43 | + OutputStream os = conn.getOutputStream(); |
| 44 | + os.write(paramStr.toString().getBytes("utf-8")); |
| 45 | + os.close(); |
| 46 | + |
| 47 | + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 48 | + String line ; |
| 49 | + String result =""; |
| 50 | + while( (line =br.readLine()) != null ){ |
| 51 | + result += "/n"+line; |
| 52 | + } |
| 53 | + System.out.println(result); |
| 54 | + br.close(); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public static void doGet(String urlStr,Map<String,Object> paramMap ) throws Exception{ |
| 59 | + String paramStr = prepareParam(paramMap); |
| 60 | + if(paramStr == null || paramStr.trim().length()<1){ |
| 61 | + |
| 62 | + }else{ |
| 63 | + urlStr +="?"+paramStr; |
| 64 | + } |
| 65 | + System.out.println(urlStr); |
| 66 | + URL url = new URL(urlStr); |
| 67 | + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
| 68 | + conn.setRequestMethod(SERVLET_PUT); |
| 69 | + conn.setRequestProperty("Content-Type","text/html; charset=UTF-8"); |
| 70 | + |
| 71 | + conn.connect(); |
| 72 | + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 73 | + String line ; |
| 74 | + String result =""; |
| 75 | + while( (line =br.readLine()) != null ){ |
| 76 | + result += "/n"+line; |
| 77 | + } |
| 78 | + System.out.println(result); |
| 79 | + br.close(); |
| 80 | + } |
| 81 | + |
| 82 | + public static void doPut(String urlStr,Map<String,Object> paramMap) throws Exception{ |
| 83 | + URL url = new URL(urlStr); |
| 84 | + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
| 85 | + conn.setRequestMethod(SERVLET_PUT); |
| 86 | + String paramStr = prepareParam(paramMap); |
| 87 | + conn.setDoInput(true); |
| 88 | + conn.setDoOutput(true); |
| 89 | + OutputStream os = conn.getOutputStream(); |
| 90 | + os.write(paramStr.toString().getBytes("utf-8")); |
| 91 | + os.close(); |
| 92 | + |
| 93 | + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 94 | + String line ; |
| 95 | + String result =""; |
| 96 | + while( (line =br.readLine()) != null ){ |
| 97 | + result += "/n"+line; |
| 98 | + } |
| 99 | + System.out.println(result); |
| 100 | + br.close(); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + public static void doDelete(String urlStr,Map<String,Object> paramMap) throws Exception{ |
| 105 | + String paramStr = prepareParam(paramMap); |
| 106 | + if(paramStr == null || paramStr.trim().length()<1){ |
| 107 | + |
| 108 | + }else{ |
| 109 | + urlStr +="?"+paramStr; |
| 110 | + } |
| 111 | + System.out.println(urlStr); |
| 112 | + URL url = new URL(urlStr); |
| 113 | + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
| 114 | + conn.setDoOutput(true); |
| 115 | + conn.setRequestMethod(SERVLET_DELETE); |
| 116 | + |
| 117 | + if(conn.getResponseCode() ==200){ |
| 118 | + System.out.println("success!"); |
| 119 | + }else{ |
| 120 | + System.out.println(conn.getResponseCode()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | +} |
0 commit comments