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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
| package com.jdyun.weixin.util;
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component;
import java.security.KeyManagementException; import java.security.cert.X509Certificate; import okhttp3.ConnectionPool; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.FormBody.Builder; import okhttp3.MediaType;
@Component public class OKHttpUtil {
protected static Logger logger=LoggerFactory.getLogger(OKHttpUtil.class); protected static Logger interfaceLog = LoggerFactory.getLogger("interfaceLog");
private static OkHttpClient httpClient;
@Value("${client.backurl}") String backurl;
@Value("${client.name}") String name;
@Value("${client.secretkey}") String secretkey;
@Bean public static SSLSocketFactory getSSLSocketFactory() { try { SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, getTrustManager(), new SecureRandom()); return sslContext.getSocketFactory(); } catch (Exception e) { throw new RuntimeException(e); } }
@Bean private static TrustManager[] getTrustManager() { TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { }
@Override public void checkServerTrusted(X509Certificate[] chain, String authType) { }
@Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } } }; return trustAllCerts; }
public static HostnameVerifier getHostnameVerifier() { HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }; return hostnameVerifier; }
public OKHttpUtil(){ if(httpClient == null){ ConnectionPool pool = new ConnectionPool(5, 50, TimeUnit.MINUTES); httpClient = new OkHttpClient.Builder() .connectTimeout(1, TimeUnit.MINUTES) .followRedirects(true) .readTimeout(1, TimeUnit.MINUTES) .retryOnConnectionFailure(false) .writeTimeout(3, TimeUnit.MINUTES) .connectionPool(pool) .sslSocketFactory(getSSLSocketFactory()) .hostnameVerifier(getHostnameVerifier()) .build(); } }
private void config(Request.Builder builder) { builder.addHeader("User-Agent", "Mozilla/5.0"); builder.addHeader("Accept","text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8"); builder.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); builder.addHeader("Accept-Charset", "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7"); builder.addHeader("Connection", "close"); builder.addHeader("secretkey", secretkey); builder.addHeader("name", name); }
public String httpGet(Map paramurl,String url) throws MalformedURLException, UnsupportedEncodingException, NoSuchAlgorithmException{ String strUrl=backurl+url; String addres = ""; int i = 0; for (Map.Entry entry : paramurl.entrySet()) { if(i == 0){ addres += "?"+entry.getKey() +"="+entry.getValue(); }else{ addres += "&"+entry.getKey() +"="+entry.getValue(); } i++; } strUrl += addres; URL u=new URL(strUrl);
Request.Builder builder =new Request.Builder(); config(builder);
String rtn="{\"status\":\"fail\"}";
try { Request request = builder.url(u).get() .build(); Response response = httpClient.newCall(request).execute(); if(response.isSuccessful()){ rtn = response.body().string(); interfaceLog.info("系统"+url+"接口result===============>>"+rtn); } } catch (Exception e) { logger.error("系统"+url+"error===============>>"+e); }finally{ } return rtn; }
public String httpPost(Map map,String strurl) { strurl=backurl+strurl; String result = null; Request.Builder builder =new Request.Builder(); config(builder);
Builder body = new FormBody.Builder();
for (Map.Entry entry : map.entrySet()) { body.add(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString()); }
FormBody formbody = body.build(); try { Request request = builder.url(strurl).post(formbody) .build(); Response response = httpClient.newCall(request).execute(); if(response.isSuccessful()){ result = response.body().string(); interfaceLog.info("系统"+strurl+"接口result===============>>"+result); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ } return result; }
public String httpGetForJSTicket(String paramurl,String url) throws MalformedURLException, UnsupportedEncodingException, NoSuchAlgorithmException{ url += paramurl; URL u=new URL(url); Request.Builder builder =new Request.Builder(); config(builder);
String rtn="{\"status\":\"fail\"}";
try { Request request = builder.url(u).get() .build(); Response response = httpClient.newCall(request).execute(); if(response.isSuccessful()){ rtn = response.body().string(); interfaceLog.info("httpGetForJSTicketrtn===============>>"+rtn); } } catch (Exception e) { logger.error("httpGetForJSTicketerror===============>>"+e); }
return rtn; }
public String httpGetWebtoken(String paramurl,String url) throws MalformedURLException, UnsupportedEncodingException, NoSuchAlgorithmException{ url += paramurl; URL u=new URL(url); Request.Builder builder =new Request.Builder(); config(builder);
String rtn="{\"status\":\"fail\"}"; try { Request request = builder.url(u).get().build(); Response response = httpClient.newCall(request).execute(); Boolean para = response.isSuccessful(); interfaceLog.info("para===============>>"+para); if(para){ rtn = response.body().string(); interfaceLog.info("httpGetWebtokenrtn===============>>"+rtn); } } catch (Exception e) { logger.error("httpGetWebtokenerror===============>>"+e); } return rtn; }
public String gettoken(String url) throws MalformedURLException, UnsupportedEncodingException, NoSuchAlgorithmException{ URL u=new URL(url); Request.Builder builder =new Request.Builder(); config(builder);
String rtn="{\"status\":\"fail\"}";
try { Request request = builder.url(u).get() .build(); Response response = httpClient.newCall(request).execute(); if(response.isSuccessful()){ rtn = response.body().string(); interfaceLog.info("本地token===============>>"+rtn); } } catch (Exception e) { logger.error("gettokenerror===============>>"+e); }finally{ } return rtn; } }
|