All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.arccode.wechat.pay.api.common.util.HttpUtils Maven / Gradle / Ivy

The newest version!
package net.arccode.wechat.pay.api.common.util;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;

/**
 * 
 * Http工具, 普通使用OkHTTP; 对于需要加载证书访问的情况, 暂时使用HttpClient实现
 *
 * OkHttp官方文档并不建议我们创建多个OkHttpClient,因此全局使用一个.
 *
 * 
* * @author http://arccode.net * @since 2015-09-09 */ public class HttpUtils { private static final Logger LOG = LoggerFactory.getLogger(HttpUtils.class); private static final OkHttpClient mOkHttpClient = new OkHttpClient(); //表示请求器是否已经做了初始化工作 private static boolean hasInit = false; //连接超时时间,默认10秒 private static int socketTimeout = 10000; //请求器的配置 private static RequestConfig requestConfig; //传输超时时间,默认30秒 private static int connectTimeout = 30000; //HTTP请求器 private static CloseableHttpClient httpClient; static { mOkHttpClient.setConnectTimeout(30, TimeUnit.SECONDS); } /** * 初始化 SSL * * @throws IOException * @throws KeyStoreException * @throws UnrecoverableKeyException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ private static void init(String certPwd, String certPath) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(certPath));//加载本地的证书进行https加密传输 try { keyStore.load(instream, certPwd.toCharArray());//设置证书密码 } catch (CertificateException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom() .loadKeyMaterial(keyStore, certPwd.toCharArray()) .build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); httpClient = HttpClients.custom() .setSSLSocketFactory(sslsf) .build(); //根据默认超时限制初始化requestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build(); hasInit = true; } /** * 该不会开启异步线程。 * * @param request * @return * @throws java.io.IOException */ public static Response execute(Request request) throws IOException { return mOkHttpClient.newCall(request).execute(); } /** * 带证书发送请求, 该不会开启异步线程。 * * @param url * @param body * @param certPwd * @param certPath * @return * @throws Exception */ public static String executeAttachCA(String url, String body, String certPwd, String certPath) throws Exception { if (!hasInit) { init(certPwd, certPath); } String result = null; HttpPost httpPost = new HttpPost(url); //解决XStream对出现双下划线的bug LOG.info("API,POST过去的数据是:"); LOG.info(body); //得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别 StringEntity postEntity = new StringEntity(body, "UTF-8"); httpPost.addHeader("Content-Type", "text/xml"); httpPost.setEntity(postEntity); //设置请求器的配置 httpPost.setConfig(requestConfig); LOG.info("executing request" + httpPost.getRequestLine()); try { HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity, "UTF-8"); } catch (ConnectionPoolTimeoutException e) { LOG.warn("http get throw ConnectionPoolTimeoutException(wait time out)"); } catch (ConnectTimeoutException e) { LOG.warn("http get throw ConnectTimeoutException"); } catch (SocketTimeoutException e) { LOG.warn("http get throw SocketTimeoutException"); } catch (Exception e) { LOG.warn("http get throw Exception"); } finally { httpPost.abort(); } return result; } /** * 开启异步线程访问网络 * * @param request * @param responseCallback */ public static void enqueue(Request request, Callback responseCallback) { mOkHttpClient.newCall(request).enqueue(responseCallback); } /** * 开启异步线程访问网络, 且不在意返回结果(实现空callback) * * @param request */ public static void enqueue(Request request) { mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onResponse(Response arg0) throws IOException { } @Override public void onFailure(Request arg0, IOException arg1) { } }); } public static String getStringFromServer(String url) throws IOException { Request request = new Request.Builder().url(url).build(); Response response = execute(request); if (response.isSuccessful()) { String responseUrl = response.body().string(); return responseUrl; } else { throw new IOException("Unexpected code " + response); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy