|
| 1 | +package com.sekift.logger.util; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.io.OutputStreamWriter; |
| 8 | +import java.net.HttpURLConnection; |
| 9 | +import java.net.URL; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * 提供通过HTTP协议获取内容的方法 <br/> |
| 18 | + * 所有提供方法中的params参数在内部不会进行自动的url encode,如果提交参数需要进行url encode, |
| 19 | + * 请调用方自行处理 |
| 20 | + * @date 2018-05-18 |
| 21 | + */ |
| 22 | +@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 23 | +public class HttpUtil { |
| 24 | + private static Logger logger = LoggerFactory.getLogger(HttpUtil.class); |
| 25 | + |
| 26 | + /** |
| 27 | + * 支持的Http method |
| 28 | + * |
| 29 | + */ |
| 30 | + private static enum HttpMethod { |
| 31 | + POST,DELETE,GET,PUT,HEAD; |
| 32 | + }; |
| 33 | + |
| 34 | + private static String invokeUrl(String url, Map params, Map<String,String> headers, int connectTimeout, int readTimeout, String encoding, HttpMethod method){ |
| 35 | + //构造请求参数字符串 |
| 36 | + StringBuilder paramsStr = null; |
| 37 | + if(params != null){ |
| 38 | + paramsStr = new StringBuilder(); |
| 39 | + Set<Map.Entry> entries = params.entrySet(); |
| 40 | + for(Map.Entry entry:entries){ |
| 41 | + String value = (entry.getValue()!=null)?(String.valueOf(entry.getValue())):""; |
| 42 | + paramsStr.append(entry.getKey() + "=" + value + "&"); |
| 43 | + } |
| 44 | + //只有POST方法才能通过OutputStream(即form的形式)提交参数 |
| 45 | + if(method != HttpMethod.POST){ |
| 46 | + url += "?"+paramsStr.toString(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + URL uUrl = null; |
| 51 | + HttpURLConnection conn = null; |
| 52 | + BufferedWriter out = null; |
| 53 | + BufferedReader in = null; |
| 54 | + try { |
| 55 | + //创建和初始化连接 |
| 56 | + uUrl = new URL(url); |
| 57 | + conn = (HttpURLConnection) uUrl.openConnection(); |
| 58 | + conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); |
| 59 | + conn.setRequestMethod(method.toString()); |
| 60 | + conn.setDoOutput(true); |
| 61 | + conn.setDoInput(true); |
| 62 | + //设置连接超时时间 |
| 63 | + conn.setConnectTimeout(connectTimeout); |
| 64 | + //设置读取超时时间 |
| 65 | + conn.setReadTimeout(readTimeout); |
| 66 | + //指定请求header参数 |
| 67 | + if(headers != null && headers.size() > 0){ |
| 68 | + Set<String> headerSet = headers.keySet(); |
| 69 | + for(String key:headerSet){ |
| 70 | + conn.setRequestProperty(key, headers.get(key)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if(paramsStr != null && method == HttpMethod.POST){ |
| 75 | + //发送请求参数 |
| 76 | + out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(),encoding)); |
| 77 | + out.write(paramsStr.toString()); |
| 78 | + out.flush(); |
| 79 | + } |
| 80 | + |
| 81 | + //接收返回结果 |
| 82 | + StringBuilder result = new StringBuilder(); |
| 83 | + in = new BufferedReader(new InputStreamReader(conn.getInputStream(),encoding)); |
| 84 | + if(in != null){ |
| 85 | + String line = ""; |
| 86 | + while ((line = in.readLine()) != null) { |
| 87 | + result.append(line); |
| 88 | + } |
| 89 | + } |
| 90 | + return result.toString(); |
| 91 | + } catch (Exception e) { |
| 92 | + logger.error("调用接口["+url+"]失败!请求URL:"+url+",参数:"+params,e); |
| 93 | + //处理错误流,提高http连接被重用的几率 |
| 94 | + try { |
| 95 | + byte[] buf = new byte[100]; |
| 96 | + InputStream es = conn.getErrorStream(); |
| 97 | + if(es != null){ |
| 98 | + while (es.read(buf) > 0) {;} |
| 99 | + es.close(); |
| 100 | + } |
| 101 | + } catch (Exception e1) { |
| 102 | + e1.printStackTrace(); |
| 103 | + } |
| 104 | + } finally { |
| 105 | + try { |
| 106 | + if (out!=null) { |
| 107 | + out.close(); |
| 108 | + } |
| 109 | + }catch (Exception e) { |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + try { |
| 113 | + if (in !=null) { |
| 114 | + in.close(); |
| 115 | + } |
| 116 | + }catch (Exception e) { |
| 117 | + e.printStackTrace(); |
| 118 | + } |
| 119 | + //关闭连接 |
| 120 | + if (conn != null){ |
| 121 | + conn.disconnect(); |
| 122 | + } |
| 123 | + } |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * POST方法提交Http请求,语义为“增加” <br/> |
| 129 | + * 注意:Http方法中只有POST方法才能使用body来提交内容 |
| 130 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 131 | + * @param params 参数 |
| 132 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 133 | + * @param readTimeout 读取超时时间(单位为ms) |
| 134 | + * @param charset 字符集(一般该为“utf-8”) |
| 135 | + * @return |
| 136 | + */ |
| 137 | + public static String post(String url, Map params, int connectTimeout, int readTimeout, String charset){ |
| 138 | + return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.POST); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * POST方法提交Http请求,语义为“增加” <br/> |
| 143 | + * 注意:Http方法中只有POST方法才能使用body来提交内容 |
| 144 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 145 | + * @param params 参数 |
| 146 | + * @param headers 请求头参数 |
| 147 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 148 | + * @param readTimeout 读取超时时间(单位为ms) |
| 149 | + * @param charset 字符集(一般该为“utf-8”) |
| 150 | + * @return |
| 151 | + */ |
| 152 | + public static String post(String url, Map params, Map<String,String> headers, int connectTimeout, int readTimeout, String charset){ |
| 153 | + return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.POST); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * GET方法提交Http请求,语义为“查询” |
| 158 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 159 | + * @param params 参数 |
| 160 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 161 | + * @param readTimeout 读取超时时间(单位为ms) |
| 162 | + * @param charset 字符集(一般该为“utf-8”) |
| 163 | + * @return |
| 164 | + */ |
| 165 | + public static String get(String url, Map params, int connectTimeout, int readTimeout, String charset){ |
| 166 | + return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.GET); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * GET方法提交Http请求,语义为“查询” |
| 171 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 172 | + * @param params 参数 |
| 173 | + * @param headers 请求头参数 |
| 174 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 175 | + * @param readTimeout 读取超时时间(单位为ms) |
| 176 | + * @param charset 字符集(一般该为“utf-8”) |
| 177 | + * @return |
| 178 | + */ |
| 179 | + public static String get(String url, Map params, Map<String,String> headers,int connectTimeout, int readTimeout, String charset){ |
| 180 | + return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.GET); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * PUT方法提交Http请求,语义为“更改” <br/> |
| 185 | + * 注意:PUT方法也是使用url提交参数内容而非body,所以参数最大长度收到服务器端实现的限制,Resin大概是8K |
| 186 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 187 | + * @param params 参数 |
| 188 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 189 | + * @param readTimeout 读取超时时间(单位为ms) |
| 190 | + * @param charset 字符集(一般该为“utf-8”) |
| 191 | + * @return |
| 192 | + */ |
| 193 | + public static String put(String url, Map params, int connectTimeout, int readTimeout, String charset){ |
| 194 | + return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.PUT); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * PUT方法提交Http请求,语义为“更改” <br/> |
| 199 | + * 注意:PUT方法也是使用url提交参数内容而非body,所以参数最大长度收到服务器端实现的限制,Resin大概是8K |
| 200 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 201 | + * @param params 参数 |
| 202 | + * @param headers 请求头参数 |
| 203 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 204 | + * @param readTimeout 读取超时时间(单位为ms) |
| 205 | + * @param charset 字符集(一般该为“utf-8”) |
| 206 | + * @return |
| 207 | + */ |
| 208 | + public static String put(String url, Map params, Map<String,String> headers,int connectTimeout, int readTimeout, String charset){ |
| 209 | + return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.PUT); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * DELETE方法提交Http请求,语义为“删除” |
| 214 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 215 | + * @param params 参数 |
| 216 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 217 | + * @param readTimeout 读取超时时间(单位为ms) |
| 218 | + * @param charset 字符集(一般该为“utf-8”) |
| 219 | + * @return |
| 220 | + */ |
| 221 | + public static String delete(String url, Map params, int connectTimeout, int readTimeout, String charset){ |
| 222 | + return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.DELETE); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * DELETE方法提交Http请求,语义为“删除” |
| 227 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 228 | + * @param params 参数 |
| 229 | + * @param headers 请求头参数 |
| 230 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 231 | + * @param readTimeout 读取超时时间(单位为ms) |
| 232 | + * @param charset 字符集(一般该为“utf-8”) |
| 233 | + * @return |
| 234 | + */ |
| 235 | + public static String delete(String url, Map params, Map<String,String> headers, int connectTimeout, int readTimeout, String charset){ |
| 236 | + return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.DELETE); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * HEAD方法提交Http请求,语义同GET方法 <br/> |
| 241 | + * 跟GET方法不同的是,用该方法请求,服务端不返回message body只返回头信息,能节省带宽 |
| 242 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 243 | + * @param params 参数 |
| 244 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 245 | + * @param readTimeout 读取超时时间(单位为ms) |
| 246 | + * @param charset 字符集(一般该为“utf-8”) |
| 247 | + * @return |
| 248 | + */ |
| 249 | + public static String head(String url, Map params, int connectTimeout, int readTimeout, String charset){ |
| 250 | + return invokeUrl(url,params,null,connectTimeout,readTimeout,charset,HttpMethod.HEAD); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * HEAD方法提交Http请求,语义同GET方法 <br/> |
| 255 | + * 跟GET方法不同的是,用该方法请求,服务端不返回message body只返回头信息,能节省带宽 |
| 256 | + * @param url 资源路径(如果url中已经包含参数,则params应该为null) |
| 257 | + * @param params 参数 |
| 258 | + * @param headers 请求头参数 |
| 259 | + * @param connectTimeout 连接超时时间(单位为ms) |
| 260 | + * @param readTimeout 读取超时时间(单位为ms) |
| 261 | + * @param charset 字符集(一般该为“utf-8”) |
| 262 | + * @return |
| 263 | + */ |
| 264 | + public static String head(String url, Map params, Map<String,String> headers, int connectTimeout, int readTimeout, String charset){ |
| 265 | + return invokeUrl(url,params,headers,connectTimeout,readTimeout,charset,HttpMethod.HEAD); |
| 266 | + } |
| 267 | + |
| 268 | +} |
0 commit comments