https httppost ssl单向验证,json格式传输 java案例

https httppost ssl单向验证,json格式传输 java案例https httppost ssl单向验证,json格式传输 java案例

大家好,又见面了,我是你们的朋友全栈君。

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账号...

(0)


相关推荐

  • SQLyog安装_快影下载安装

    SQLyog安装_快影下载安装1、下载安装包SQLyog下载链接2、安装2.1、选择语言,默认即可2.2、下一步即可2.2、接受协议并下一步2.3、选择选项2.4、选定安装目录2.5、安装完成3、连接Mysql服务看到这个界面就连接成功了。…

  • eclipse adt bundle不显示Android SDK菜单

    eclipse adt bundle不显示Android SDK菜单我把adtbundle拷贝到装有jdk1.5的电脑时,Eclipse死活不显示SDK的相关菜单。原因是jdk版本太低,只有1.6以上才会有。。eclipse.ini里限制jdk版本原来是有意的。安装了jdk1.6后,就能定制GUI了。eclipse中的window→Customize Perspective→Command Groups availability→Av

    2022年10月25日
  • php中的<?= ?>和<?php ?>有什么区别么?

    php中的<?= ?>和<?php ?>有什么区别么?

  • LINUX最大线程数及最大进程数

    查看最大线程数:cat/proc/sys/kernel/threads-maxulimitUserlimits-limittheuseofsystem-wideresources.S

    2021年12月20日
  • oracle 中decode函数[通俗易懂]

    oracle 中decode函数[通俗易懂]Oracle中decode函数用法含义解释:decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)该函数的含义如下:IF条件=值1THEN    RETURN(翻译值1)ELSIF条件=值2THEN    RETURN(翻译值2)    …ELSIF条件=值nTHEN    RETURN(翻译值n)ELSE    RETURN(缺省…

  • LaTex学习笔记——LaTeX公式换行

    LaTex学习笔记——LaTeX公式换行ps.  1、“\!”表示其前后字符之间无间隙       2、暂留       3、段落中的数学表达式应该置于(和), $和$ 或者begin{math}和end{math} 之间。       4、对于较大的数学式子,最好的方法是使用显示式样来排版:将它们放置于[和]或begin{displaymath}和end{displaymath}之间。这样排版出的公式是没有编号…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号