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)


相关推荐

  • 平稳信号 小波变换什么意思_连续小波变换和离散小波变换

    平稳信号 小波变换什么意思_连续小波变换和离散小波变换1:什么是平稳信号首先必须了解什么叫做信号的平稳性,这个概念是诸多信号处理领域中的重要概念。如果信号的频率成分不随时间的变换而发生改变,那么该信号就被称为平稳信号。换言之,平稳信号的频率成分是稳定的,在这种情况下,不需要拍知道某一时刻信号的频率成分,因为它们已经是完全已知的了。

    2022年10月24日
  • 施密特正交化几何解释

    施密特正交化几何解释施密特正交化几何解释

  • java到大数据学习路线

    java到大数据学习路线计算机网络 操作系统 数据结构 计算机组成原理 可重点学习如下知识点计算机网络(重点看OSI七层模型或TCP/IP五层模型理解每层含义)数据结构(重点看数组、栈、队列、链表、树)算法(重点看各种排序算法、查找算法、去重算法,最优解算法,多去LeetCode刷算法题)操作系统(重点看进程、线程、IO、调度、内存管理) 数据仓库分为离线数仓和实时数仓,但是企业在招聘时大多要求两者都会,进入公司之后可能会专注于离线或实时其中之一。不…

  • 黑客学习路线[通俗易懂]

    黑客学习路线[通俗易懂]转载:https://www.cnblogs.com/HackKen/p/7732515.html引:在任何时候,我都是孤独的。最近在信安群里也有人问过我,我是如何学习黑客和渗透测试的,在这里,我就把我的学习路线写一下,让新手和小白们不再迷茫,少走弯路,拒绝时间和金钱上的浪费!第一周:入门在学习的时候,你起码需要对常见的黑客术语需要掌握,了解术语的话可以去看看这里的百度文库,写的还是可…

  • 第一篇提问艺术「建议收藏」

    第一篇提问艺术「建议收藏」虽然这是老话常谈,但是最近的回答问题的过程中,有点感触。你问题问的好,问的准确,回答你的人才有积极性给你答复,这样你又可以更快的解决你的问题。好多人不知道如何提问,所以我打算把这篇老文章转过来置顶。花点时间看看。如果没时间,至少看看下面的图吧:====================================================================

  • 基于Python的频谱分析(一)

    基于Python的频谱分析(一)

    2021年11月21日

发表回复

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

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