package com.lerry.swaggershowdoc.util; import lombok.extern.slf4j.Slf4j; import okhttp3.*; import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.http.util.TextUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.Map; @Slf4j @Component public class OkHttpUtil { @Autowired private OkHttpClient okHttpClient; /** * get * * @param url 请求的url * @param queries 请求的参数,在浏览器?后面的数据,没有可以传null * @return */ public String get(String url, Map<String, String> queries) { StringBuffer sb = new StringBuffer(url); this.doQueries(sb, queries); Request request = new Request.Builder() .url(sb.toString()) .build(); Response response = null; return this.doHtpp(request, response); } public Response downLoadfile(String url, Map<String, String> queries) throws IOException { StringBuffer sb = new StringBuffer(url); this.doQueries(sb, queries); Request request = new Request.Builder() .url(sb.toString()) .build(); Response response = null; response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()) { return response; } return response; } private static String getHeaderFileName(Response response) { String dispositionHeader = response.header("Content-Disposition"); if (!TextUtils.isEmpty(dispositionHeader)) { dispositionHeader.replace("attachment;filename=", ""); dispositionHeader.replace("filename*=utf-8", ""); String[] strings = dispositionHeader.split("; "); if (strings.length > 1) { dispositionHeader = strings[1].replace("filename=", ""); dispositionHeader = dispositionHeader.replace("\"", ""); return dispositionHeader; } return ""; } return ""; } /** * post * * @param url 请求的url * @param params post form 提交的参数 * @return */ public String post(String url, Map<String, String> params) { FormBody.Builder builder = new FormBody.Builder(); //添加参数 if (params != null && !params.keySet().isEmpty()) { for (String key : params.keySet()) { builder.add(key, params.get(key)); } } Request request = new Request.Builder() .url(url) .post(builder.build()) .build(); Response response = null; return this.doHtpp(request, response); } /** * get * * @param url 请求的url * @param queries 请求的参数,在浏览器?后面的数据,没有可以传null * @return */ public String getForHeader(String url, Map<String, String> queries) { StringBuffer sb = new StringBuffer(url); this.doQueries(sb, queries); Request request = new Request.Builder() .addHeader("key", "value") .url(sb.toString()) .build(); Response response = null; return this.doHtpp(request, response); } /** * Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"} * 参数一:请求Url * 参数二:请求的JSON * 参数三:请求回调 */ public String postJsonParams(String url, String jsonParams) { RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); Response response = null; return this.doHtpp(request, response); } /** * Post请求发送xml数据.... * 参数一:请求Url * 参数二:请求的xmlString * 参数三:请求回调 */ public String postXmlParams(String url, String xml) { RequestBody requestBody = RequestBody.create(MediaType.parse("application/xml; charset=utf-8"), xml); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); Response response = null; return this.doHtpp(request, response); } private String doHtpp(Request request, Response response) { try { response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); } } catch (Exception e) { log.error("okhttp3 post error >> ex = {}", ExceptionUtils.getStackTrace(e)); } finally { httpClientClose(response); } return ""; } public void httpClientClose(Response response){ if (response != null) { ResponseBody body =response.body(); if(body == null || body.contentLength() == 0){ if(body != null){ body.close(); } } response.close(); } } private void doQueries(StringBuffer sb, Map<String, String> queries) { if (queries != null && !queries.keySet().isEmpty()) { boolean firstFlag = true; Iterator<Map.Entry<String, String>> iterator = queries.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); if (firstFlag) { sb.append("?").append(entry.getKey()).append("=").append(entry.getValue()); firstFlag = false; } else { sb.append("&").append(entry.getKey()).append("=").append(entry.getValue()); } } } } }