大家好,又见面了,我是你们的朋友全栈君。
package util;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.apache.commons.httpclient.HttpException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
*
* @author liuxin
* @date 2018年6月28日
*/
public class HttpsClientDemo {
public static void main(String[] args) {
String url="https://10.193.16.66:10031/nf-front";
String json = "{\"CHANNEL_ID\":\"9979\",\"TEST_FLAG\":\"Y\",\"VERSION\":\"01\",\"SIGN\":\"\",\"CONTEXT\":{ \"SERVICE\": { \"SERVICE_HEADER\": { \"SERVICE_ID\": \"TFNCommodyLoanSetupWithDraw\", \"ORG\": \"9979\", \"CHANNEL_ID\": \"BANK\", \"ACQ_ID\": \"20000015\", \"SUB_TERMINAL_TYPE\": \"APP\", \"SERVICESN\": \"qx2017081527298756602\", \"OP_ID\": null, \"REQUEST_TIME\": \"20180729024927\", \"VERSION_ID\": \"01\", \"MAC\": null, \"SERV_RESPONSE\": null }, \"SERVICE_BODY\": { \"REQUEST\": { \"CUSTOMER_NAME\": \"测试07260002\", \"UUID\": \"2017072601004002\", \"SEX\": \"M\", \"BIRTHDAY\": \"19760926\", \"MARRIAGE\": \"C\", \"COUNTRY\": \"CN\", \"CERT_ID\": \"110101198001080035\", \"CERT_TYPE\": \"I\", \"DUE_DATE\": \"20170710\", \"MOBILE\": \"15062650860\", \"FAMILY_STATUS\": \"A\", \"FAMILY_TEL\": \"021-6228126\", \"EDU_EXPERIENCE\": \"A\", \"EDU_DEGREE\": \"A\", \"EMAIL\": \"12345@qq.com\", \"ADDRESSES\": [ { \"ZIP_CODE\": \"200000\", \"ADDRESS_TYPE\": \"H\", \"COUNTRY_CODE\": \"156\", \"PROVINCE\": \"上海市\", \"CITY\": \"上海市\", \"AREA_CODE\": \"浦东新区\", \"ADDR\": \"顾唐路龙东大道\", \"TEL\": \"021-99889876\" }, { \"ZIP_CODE\": \"210000\", \"ADDRESS_TYPE\": \"C\", \"COUNTRY_CODE\": \"156\", \"PROVINCE\": \"南京市\", \"CITY\": \"南京市\", \"AREA_CODE\": \"浦东新区\", \"ADDR\": \"夫子庙大厦\", \"TEL\": \"021-37876656\" }, { \"ZIP_CODE\": \"214000\", \"ADDRESS_TYPE\": \"S\", \"COUNTRY_CODE\": \"156\", \"PROVINCE\": \"无锡市\", \"CITY\": \"无锡市\", \"AREA_CODE\": \"鼓楼区\", \"ADDR\": \"dddd\", \"TEL\": \"010-22223333\" } ], \"WORK_CORP\": \"CUPDtest\", \"WORK_TEL\": \"568971463\", \"CURR_WORK_YEARS\": \"1\", \"WORK_YEARS\": \"4\", \"POSITION_LEVEL\": \"A\", \"POSITION\": \"A\", \"WORKNATURE\": \"A\", \"UNITKIND\": \"A\", \"LINK1\": \"李四\", \"LINKRELATION1\": \"F\", \"LINKMOBILE1\": \"13244556714\", \"LINK2\": null, \"LINKRELATION2\": null, \"LINKMOBILE2\": null, \"APPLY_DATE\": \"20170605\", \"APPLY_NO\": \"1707260001200002\", \"CONTRA_EXPIRE_DATE\": null, \"BUSINESS_TYPE\": \"MCEP\", \"LOAN_CODE\": \"1006\", \"LOAN_FEE_DEF_ID\": \"\", \"LOAN_AMT\": 30000.01, \"LOAN_TERM\": \"12\", \"PURPOSE\": \"120\", \"INCOME_FLAG\": null, \"MONTHLY_WAGES\": 22222, \"BANK_CARD_NBR\": \"6212260726165000052\", \"BANK_CARD_NAME\": \"测试07260002\", \"BANK_CODE\": \"0102\", \"BANK_NAME\": \"工商银行\", \"BANK_PROVINCE\": \"吉林省\", \"BANK_PROV_CODE\": \"110000\", \"BANK_CITY\": \"通化市\", \"BANK_CITY_CODE\": \"110101\", \"AGREE_RATE\": null, \"AGREE_RATE_EXPIRE_DATE\": null, \"JOIN_LIFE_INSU_IND\": \"Y\", \"COOPERATOR_ID\": \"20000000\", \"AGREEMENT_RATE_IND\": \"N\", \"FEE_RATE\": null, \"FEE_AMOUNT\": null, \"LIFE_INSU_FEE_RATE\": null, \"LIFE_INSU_FEE_AMT\": null, \"AGENT_FEE_RATE\": null, \"AGENT_FEE_AMOUNT\": null, \"INS_RATE\": null, \"INS_AMT\": null, \"INSTALLMENT_FEE_RATE\": null, \"INSTALLMENT_FEE_AMT\": null, \"PREPAYMENT_FEE_AMOUNT_RATE\": null, \"PREPAYMENT_FEE_AMOUNT\": null, \"PENALTY_RATE\": null, \"COMPOUND_RATE\": null, \"INTEREST_RATE\": null, \"MER_ID\": null, \"MER_NAME\": null, \"MERCHANDISE_AMT\": \"500099\", \"RA_ID\": \"999999\", \"AUTH_TXN_TERMINAL\": \"MSCM\", \"MERCHANDISE_ORDER\": \"2205714\", \"DOWN_PAYMENT_AMT\": \"10000\", \"STAMPDUTY_RATE\": null, \"APPLY_NO_IS_CRED_NO\": \"Y\", \"CUSTOMER_NAME_CN\": null, \"CUSTOMER_USED_NAME\": \"\", \"RANK\": null, \"CUSTOMER_BANK_CODE\": \"0321\", \"LOAN_BANK_CODE\": \"629\", \"ON_OFF_SHORE\": \"O\", \"PROMOTION_CODE\": \"\", \"REPAY_CYCLE\": \"\", \"AB_ACCESS\": null, \"AB_BRANCH\": null, \"AB_USER\": null, \"AB_NAME\": null, \"CUSTOMER_BRANCH_NO\": \"502\", \"YEARLY_INCOME\": 30000, \"USED_NAME\": null, \"CUST_SERIAL_NO\": \"2017072601004002\", \"PAY_ONLINE\": \"Y\", \"GUARANTY_ID\": null, \"COINST_CODE\": null, \"COINST_CHANNEL\": null, \"PAY_MODE\": \"N\", \"APPLY_CITY\": "
+ "\"\", \"STMTADDR_TYPE\": \"H\", \"USER_CODE2\": null, \"USER_CODE3\": null, \"USER_FIELD1\": null, \"ACCT_FIELD1\": \"6212260726165000052\", \"ACCT_FIELD2\": \"0102\", \"ACCT_FIELD3\": null }, \"RESPONSE\": null } } }}";
String charSet="utf-8";
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = new SSLClient3();
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", SSLClient3.socketFactory, 443));
httpPost = new HttpPost(url);
JSONObject map = JSON.parseObject(json);
StringEntity entity = new StringEntity(map.toString(), charSet);
entity.setContentEncoding(charSet);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse response;
response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charSet);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result);
}
}
class SSLClient3 extends DefaultHttpClient{
public static String keyStorePassword="liuxin";
public static String keyStorePath="E:/eclipse_work/tomcat-key/tomcat.keystore";
public static SSLSocketFactory socketFactory;
public SSLClient3() throws Exception{
SSLContext context = null;
KeyStore ts = KeyStore.getInstance("JKS");
ts.load(new FileInputStream(keyStorePath), keyStorePassword.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
TrustManager [] tm = tmf.getTrustManagers();
context = SSLContext.getInstance("SSL");
context.init(null, tm, null);
socketFactory = new SSLSocketFactory(context);
socketFactory.setHostnameVerifier( ignoreHostnameVerifier);
}
private static X509HostnameVerifier ignoreHostnameVerifier = new X509HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
@Override
public void verify(String host, SSLSocket ssl) throws IOException {
}
@Override
public void verify(String host, X509Certificate cert) throws SSLException {
}
@Override
public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
}
};
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106088.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...