1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| /** * @author zllwsy * @Description OkHttp工具类 * @Date 2021/6/30 14:42 * @Created by zllwsy */ @Component public class OkHttpUtil {
private static String url = null; //从yml配置文件读取ip @Value("${OkHttp.url}") public void setUrl(String url) { OkHttpUtil.url = url; }
public String getUrl() { return url; }
private static final Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);
/** * 根据map获取get请求参数 * @param queries * @return */ public static StringBuffer getQueryString(String url,Map<String,String> queries){ StringBuffer sb = new StringBuffer(url); if (queries != null && queries.keySet().size() > 0) { boolean firstFlag = true; Iterator iterator = queries.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry<String, String>) iterator.next(); if (firstFlag) { sb.append("?" + entry.getKey() + "=" + entry.getValue()); firstFlag = false; } else { sb.append("&" + entry.getKey() + "=" + entry.getValue()); } } } return sb; }
/** * 调用okhttp的newCall方法 * @param request * @return */ private static String execNewCall(Request request){ Response response = null; try { OkHttpClient okHttpClient = SpringUtil.getBean(OkHttpClient.class); response = okHttpClient.newCall(request).execute(); int status = response.code(); if (response.isSuccessful()) { return response.body().string(); } } catch (Exception e) { logger.error("okhttp3 put error >> ex = {}", ExceptionUtils.getStackTrace(e)); } finally { if (response != null) { response.close(); } } return ""; }
/** * get * @param path path 请求的url * @param queries 请求的参数,在浏览器?后面的数据,没有可以传null * @return */ public static String get(String path, Map<String, String> queries) { if (!path.startsWith("/")){ path = "/" + path; } url = url + path; StringBuffer sb = getQueryString(url,queries); Request request = new Request.Builder() .url(sb.toString()) .build(); return execNewCall(request); }
/** * post * * @param path path 请求的url * @param params post form 提交的参数 * @return */ public static String postFormParams(String path, Map<String, String> params) { if (!path.startsWith("/")){ path = "/" + path; } url = url + path; FormBody.Builder builder = new FormBody.Builder(); //添加参数 if (params != null && params.keySet().size() > 0) { for (String key : params.keySet()) { builder.add(key, params.get(key)); } } Request request = new Request.Builder() .url(url) .post(builder.build()) .build(); return execNewCall(request); }
/** * Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"} * 参数一:请求Url * 参数二:请求的JSON * 参数三:请求回调 */ public static String postJsonParams(String path, String jsonParams) { if (!path.startsWith("/")){ path = "/" + path; } url = url + path; RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); return execNewCall(request); }
/** * Post请求发送xml数据.... * 参数一:请求Url * 参数二:请求的xmlString * 参数三:请求回调 */ public static String postXmlParams(String path, String xml) { if (!path.startsWith("/")){ path = "/" + path; } url = url + path; RequestBody requestBody = RequestBody.create(MediaType.parse("application/xml; charset=utf-8"), xml); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); return execNewCall(request); } }
|