|
- package com.iformall.utils;
-
- import okhttp3.MediaType;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import org.apache.commons.io.IOUtils;
- import org.apache.http.*;
- import org.apache.http.client.CookieStore;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.BasicCookieStore;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.ssl.SSLContexts;
- import org.apache.http.util.EntityUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import com.github.binarywang.wxpay.service.WxPayService;
-
- import cn.binarywang.wx.miniapp.api.WxMaService;
-
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.KeyManager;
- import javax.net.ssl.KeyManagerFactory;
- import javax.net.ssl.SSLContext;
- import java.io.*;
- import java.net.URI;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.security.KeyManagementException;
- import java.security.KeyStore;
- import java.security.KeyStoreException;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- import java.security.UnrecoverableKeyException;
- import java.security.cert.CertificateException;
- import java.util.*;
- import java.util.concurrent.ConcurrentHashMap;
-
-
- /**
- * @author
- * @date
- * HttpClient工具类
- */
- public class HttpUtil {
-
- private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
-
- private static final MediaType CONTENT_TYPE_FORM = MediaType.parse("application/x-www-form-urlencoded");
- private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
-
-
- // /**
- // * get请求
- // * @return
- // */
- // public static String doGet(String url) {
- // try {
- // CloseableHttpClient client = HttpClients.createDefault();
- // //发送get请求
- // HttpGet request = new HttpGet(url);
- // HttpResponse response = client.execute(request);
- //
- // /**请求发送成功,并得到响应**/
- // if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- // /**读取服务器返回过来的json字符串数据**/
- // String strResult = EntityUtils.toString(response.getEntity());
- //
- // return strResult;
- // }
- // }
- // catch (IOException e) {
- // logger.error(e.getMessage());
- // throw new RuntimeException(e);
- // }
- //
- // return null;
- // }
-
- /**
- * get请求
- * @return
- */
- public static byte[] doWiwidePicGet(String url, String signature) {
- try {
- CloseableHttpClient client = HttpClients.createDefault();
- //发送get请求
- HttpGet request = new HttpGet(url);
- request.setHeader("Cookie", "Signature="+signature);
-
- HttpResponse response = client.execute(request);
-
- /**请求发送成功,并得到响应**/
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- /**读取服务器返回过来的json字符串数据**/
-
- byte[] bytes = EntityUtils.toByteArray(response.getEntity());
-
- return bytes;
- }
- }
- catch (IOException e) {
- logger.error(e.getMessage());
- throw new RuntimeException(e);
- }
-
- return null;
- }
-
- /**
- * post请求(用于key-value格式的参数)
- * @param url
- * @param params
- * @return
- */
- public static String doPost(String url, Map params){
-
- // 定义HttpClient
- CloseableHttpClient client = HttpClients.createDefault();
-
- BufferedReader in = null;
- try {
-
- // 实例化HTTP方法
- HttpPost request = new HttpPost();
- request.setURI(new URI(url));
-
- //设置参数
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
- for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
- String name = (String) iter.next();
- String value = String.valueOf(params.get(name));
- nvps.add(new BasicNameValuePair(name, value));
-
- //System.out.println(name +"-"+value);
- }
- request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
-
- HttpResponse response = client.execute(request);
- int code = response.getStatusLine().getStatusCode();
- if(code == 200){ //请求成功
- in = new BufferedReader(new InputStreamReader(response.getEntity()
- .getContent(),"utf-8"));
- StringBuilder sb = new StringBuilder("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
-
- in.close();
-
- client.close();
-
- return sb.toString();
- }
- else{ //
- logger.info("状态码:" + code);
- client.close();
- }
- }
- catch(Exception e){
- logger.error(e.getMessage());
- return null;
- }
-
- return null;
- }
-
- // /**
- // * post请求(用于key-value格式的参数)
- // * @param url
- // * @param params
- // * @return
- // */
- // public static String doPost(String url,Map<String,String> headMap, Map params){
- //
- // // 定义HttpClient
- // CloseableHttpClient client = HttpClients.createDefault();
- //
- // BufferedReader in = null;
- // try {
- //
- // // 实例化HTTP方法
- // HttpPost request = new HttpPost();
- // request.setURI(new URI(url));
- // if(headMap != null){
- // Set<String> set = headMap.keySet();
- // for(String key: set){
- // request.addHeader(key,headMap.get(key));
- // }
- // }
- //
- // //设置参数
- // List<NameValuePair> nvps = new ArrayList<NameValuePair>();
- // for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
- // String name = (String) iter.next();
- // String value = String.valueOf(params.get(name));
- // nvps.add(new BasicNameValuePair(name, value));
- //
- // //System.out.println(name +"-"+value);
- // }
- // request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
- //
- // HttpResponse response = client.execute(request);
- // int code = response.getStatusLine().getStatusCode();
- // if(code == 200){ //请求成功
- // in = new BufferedReader(new InputStreamReader(response.getEntity()
- // .getContent(),"utf-8"));
- // StringBuilder sb = new StringBuilder("");
- // String line = "";
- // String NL = System.getProperty("line.separator");
- // while ((line = in.readLine()) != null) {
- // sb.append(line + NL);
- // }
- //
- // in.close();
- //
- // client.close();
- //
- // return sb.toString();
- // }
- // else{ //
- // logger.info("状态码:" + code);
- // client.close();
- // }
- // }
- // catch(Exception e){
- // logger.error(e.getMessage());
- // return null;
- // }
- //
- // return null;
- // }
-
- /**
- * post请求(用于key-value格式的参数,wiwide)
- * @param url
- * @param params
- * @return
- */
- public static String doPostWiwide(String url, String token, Map params){
-
- //CookieStore store= new BasicCookieStore();
- //HttpClients.custom().setDefaultCookieStore(store).build();
-
- // 定义HttpClient
- CloseableHttpClient client = HttpClients.createDefault();
-
-
- BufferedReader in = null;
- try {
-
- // 实例化HTTP方法
- HttpPost request = new HttpPost();
- request.setURI(new URI(url));
- request.setHeader("Content-Type", "application/x-www-form-urlencoded");
- request.setHeader("Accept-Charset", "utf-8");
- request.setHeader("wiwidefumaotoken", token);
-
- //设置参数
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
- for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
- String name = (String) iter.next();
- String value = String.valueOf(params.get(name));
- nvps.add(new BasicNameValuePair(name, value));
-
- //System.out.println(name +"-"+value);
- }
- request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
-
- HttpResponse response = client.execute(request);
- int code = response.getStatusLine().getStatusCode();
- if(code == 200){ //请求成功
- in = new BufferedReader(new InputStreamReader(response.getEntity()
- .getContent(),"utf-8"));
- StringBuilder sb = new StringBuilder("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
-
- in.close();
-
- client.close();
-
- return sb.toString();
- }
- else{ //
- logger.info("状态码:" + code);
- client.close();
- }
- }
- catch(Exception e){
- logger.error(e.getMessage());
- return null;
- }
-
- return null;
- }
-
- /**
- * post请求(用于key-value格式的参数,wiwide)
- * @param url
- * @param params
- * @return
- */
- public static String doPostWiwideNew(String url, String signature, Map params){
-
- //CookieStore store= new BasicCookieStore();
- //HttpClients.custom().setDefaultCookieStore(store).build();
-
- // 定义HttpClient
- CloseableHttpClient client = HttpClients.createDefault();
-
-
- BufferedReader in = null;
- try {
-
- // 实例化HTTP方法
- HttpPost request = new HttpPost();
- request.setURI(new URI(url));
- request.setHeader("Content-Type", "application/x-www-form-urlencoded");
- request.setHeader("Accept-Charset", "utf-8");
- request.setHeader("Cookie", "Signature="+signature);
-
- //设置参数
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
- for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
- String name = (String) iter.next();
- String value = String.valueOf(params.get(name));
- nvps.add(new BasicNameValuePair(name, value));
-
- //System.out.println(name +"-"+value);
- }
- request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
-
- HttpResponse response = client.execute(request);
- int code = response.getStatusLine().getStatusCode();
- if(code == 200){ //请求成功
- in = new BufferedReader(new InputStreamReader(response.getEntity()
- .getContent(),"utf-8"));
- StringBuilder sb = new StringBuilder("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
-
- in.close();
-
- client.close();
-
- return sb.toString();
- }
- else{ //
- logger.info("状态码:" + code);
- client.close();
- }
- }
- catch(Exception e){
- logger.error(e.getMessage());
- return null;
- }
-
- return null;
- }
-
-
- /**
- * post请求(用于请求json格式的参数)
- * @param url
- * @param params
- * @return
- */
- public static String doPost(String url, Map<String,String> headMap, String params){
-
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(url);// 创建httpPost
- httpPost.setHeader("Accept", "application/json");
- httpPost.setHeader("Content-Type", "application/json");
-
- if(headMap != null){
- Set<String> set = headMap.keySet();
- for(String key: set){
- httpPost.addHeader(key,headMap.get(key));
- }
- }
-
- String charSet = "UTF-8";
- StringEntity entity = new StringEntity(params, charSet);
- httpPost.setEntity(entity);
- CloseableHttpResponse response = null;
-
- try {
- response = httpclient.execute(httpPost);
- StatusLine status = response.getStatusLine();
- int state = status.getStatusCode();
- if (state == HttpStatus.SC_OK) {
- HttpEntity responseEntity = response.getEntity();
- String jsonString = EntityUtils.toString(responseEntity);
- return jsonString;
- }
- else{
- logger.info("请求返回:"+state+"("+url+")");
- }
- }
- catch(Exception e){
- logger.error(e.getMessage());
- return null;
- }
- finally {
- if (response != null) {
- try {
- response.close();
- } catch (IOException e) {
- logger.error(e.getMessage());
- }
- }
- try {
- httpclient.close();
- } catch (IOException e) {
- logger.error(e.getMessage());
- }
- }
- return null;
- }
-
-
- public static String payPost(String url, String params) {
- RequestBody body = RequestBody.create(CONTENT_TYPE_FORM, params);
-
- Request request = null;
- try {
- request = new Request.Builder().addHeader("Accept-Charset", "utf-8")
- .addHeader("Content-Type", "application/x-www-form-urlencoded")
- .addHeader("Content-Length", String.valueOf(body.contentLength()))
- .url(url).post(body).build();
- return exec(request);
- } catch (IOException e) {
- logger.error(e.getMessage());
- return null;
- }
- }
-
- private static OkHttpClient OkHttpClient = new OkHttpClient();
-
- private static String exec(okhttp3.Request request) {
- try {
- okhttp3.Response response = OkHttpClient.newCall(request).execute();
- // String header = response.header("X-Tt-Logid");
- // logger.info("X-Tt-Logid="+header);
- if (!response.isSuccessful())
- throw new RuntimeException("Unexpected code " + response);
- return response.body().string();
- } catch (IOException e) {
- logger.error(e.getMessage());
- throw new RuntimeException(e);
- }
- }
-
- private static Map<String,SSLContext> sslContextMap = new ConcurrentHashMap<String,SSLContext>();
- private static SSLContext getSSLContext(String certPath, String certPass) throws KeyStoreException, NoSuchAlgorithmException,
- CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException, KeyManagementException {
- String key = certPath+certPass;
- SSLContext sslContext = sslContextMap.get(key);
- if ( null == sslContext ) {
- synchronized("sslContextLock"+key) {
- sslContext = sslContextMap.get(key);
- if (null == sslContext) {
- KeyStore clientStore = KeyStore.getInstance("PKCS12");
- clientStore.load(new FileInputStream(certPath), certPass.toCharArray());
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- kmf.init(clientStore, certPass.toCharArray());
- KeyManager[] kms = kmf.getKeyManagers();
- sslContext = SSLContext.getInstance("TLS");
- sslContext.init(kms, null, new SecureRandom());
- sslContextMap.put(key, sslContext);
- }
- }
- }
- return sslContext;
- }
-
- public static String payPostSSL(String url, String data, String certPath, String certPass) {
- HttpsURLConnection conn = null;
- OutputStream out = null;
- InputStream inputStream = null;
- BufferedReader reader = null;
- try {
- SSLContext sslContext = getSSLContext(certPath, certPass);
- HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
- URL _url = new URL(url);
- conn = (HttpsURLConnection) _url.openConnection();
-
- conn.setConnectTimeout(25000);
- conn.setReadTimeout(25000);
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- conn.setDoInput(true);
-
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
- conn.connect();
-
- out = conn.getOutputStream();
- out.write(data.getBytes(Charset.forName("UTF-8")));
- out.flush();
-
- inputStream = conn.getInputStream();
- reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line).append("\n");
- }
- return sb.toString();
- } catch (Exception e) {
- logger.error(e.getMessage());
- throw new RuntimeException(e);
- } finally {
- IOUtils.closeQuietly(out);
- IOUtils.closeQuietly(reader);
- IOUtils.closeQuietly(inputStream);
- if (conn != null) {
- conn.disconnect();
- }
- }
- }
-
- // private static String ClientCustomSSL(String url, String p12file, String mch_id, String xmlStr) throws Exception{
- // KeyStore keyStore = KeyStore.getInstance("PKCS12");
- // FileInputStream instream = new FileInputStream(new File(p12file));
- // try {
- // keyStore.load(instream, mch_id.toCharArray());
- // } finally {
- // instream.close();
- // }
- //
- // // Trust own CA and all self-signed certs
- // SSLContext sslcontext = SSLContexts.custom()
- // .loadKeyMaterial(keyStore, mch_id.toCharArray())
- // .build();
- // // Allow TLSv1 protocol only
- // SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
- // sslcontext,
- // new String[] { "TLSv1" },
- // null,
- // SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
- // CloseableHttpClient httpclient = HttpClients.custom()
- // .setSSLSocketFactory(sslsf)
- // .build();
- // try {
- // HttpPost httpPost = new HttpPost(url);
- // StringEntity entityStr = new StringEntity(xmlStr);
- // entityStr.setContentType("text/xml");
- // //logger.info("entityStr--------------"+entityStr);
- // httpPost.setEntity(entityStr);
- //
- // CloseableHttpResponse response = httpclient.execute(httpPost);
- // try {
- // HttpEntity entity = response.getEntity();
- //
- // //System.out.println("----------------------------------------");
- // //System.out.println(response.getStatusLine());
- // if (entity != null) {
- // //System.out.println("Response content length: " + entity.getContentLength());
- // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
- // StringBuilder sb = new StringBuilder();
- // String line = null;
- // while ((line = bufferedReader.readLine()) != null) {
- // sb.append(line).append("\n");
- // }
- // return sb.toString();
- // }
- // EntityUtils.consume(entity);
- // } finally {
- // response.close();
- // }
- // } finally {
- // httpclient.close();
- // return null;
- // }
- //
- // }
-
- /**
- * post请求(用于请求json格式的参数)
- * @param url
- * @param params
- * @return
- */
- public static String doAiVideoPost(String url, String params){
-
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(url);// 创建httpPost
- httpPost.setConfig(RequestConfig.custom()
- .setConnectionRequestTimeout(5000)
- .setConnectTimeout(3000000)
- .setSocketTimeout(3000000)
- .build());
- httpPost.setHeader("Accept", "application/json");
- httpPost.setHeader("Content-Type", "application/json");
- String charSet = "UTF-8";
- StringEntity entity = new StringEntity(params, charSet);
- httpPost.setEntity(entity);
- CloseableHttpResponse response = null;
-
- try {
- response = httpclient.execute(httpPost);
- StatusLine status = response.getStatusLine();
- int state = status.getStatusCode();
- if (state == HttpStatus.SC_OK) {
- HttpEntity responseEntity = response.getEntity();
- String jsonString = EntityUtils.toString(responseEntity);
- return jsonString;
- }
- else{
- logger.info("请求返回:"+state+"("+url+")");
- }
- }
- catch(Exception e){
- logger.error(e.getMessage());
- return null;
- }
- finally {
- if (response != null) {
- try {
- response.close();
- } catch (IOException e) {
- logger.error(e.getMessage());
- }
- }
- try {
- httpclient.close();
- } catch (IOException e) {
- logger.error(e.getMessage());
- }
- }
- return null;
- }
-
- }
|