httpClient4.1发送https报文请求,带证书,签名

这个方法时我测试了目前网上的commons-httpclient所提供的方法,根据HTTP请求改造的,那个测试没有通过,这个是完全根据官方最新版的httpclinet4.1的英文文档以及example来做的,比较靠谱,当前前提是你的证书已经拆分好,这个可以找到相关的很多资料,不做赘述。发送代码如下

org.apache.http.client.HttpClient hc = new org.apache.http.impl.client.DefaultHttpClient();
             List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("uerName", yourNameValue);
            nvps.add(new BasicNameValuePair("userCode", yourUserCodeValue);
            //请根据实际修改上送包xml数据,POST请求没有长度限制,get请求太长会报错,根据实际情况,一般的数据传送会要求进行签名、BASE64编码或者压缩等机制进行传输
            nvps.add(new BasicNameValuePair("reqData",getrevFromBASE64(body.getBytes("GBK"))));  
            UrlEncodedFormEntity   urlEncodedFormEntity = new UrlEncodedFormEntity(nvps,"GBK");
            String url = "https://"+yourConnectIp+":"+yourConnectPort;
            //加载证书
            java.security.KeyStore trustStore = java.security.KeyStore.getInstance(java.security.KeyStore.getDefaultType());
            //"123456"为制作证书时的密码
            trustStore.load(new FileInputStream(new File("你的证书位置")), "123456".toCharArray());
            org.apache.http.conn.ssl.SSLSocketFactory socketFactory = new org.apache.http.conn.ssl.SSLSocketFactory(trustStore);
            //不校验域名
            socketFactory.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            //这个8446是和被访问端约定的端口,一般为443
            org.apache.http.conn.scheme.Scheme sch = new org.apache.http.conn.scheme.Scheme("https", socketFactory, 8446);
            hc.getConnectionManager().getSchemeRegistry().register(sch);
            org.apache.http.client.methods.HttpPost hr = new org.apache.http.client.methods.HttpPost(url);

            hr.setEntity(urlEncodedFormEntity);
            hr.setHeader("Content-Type", "application/x-www-form-urlencoded");
            org.apache.http.HttpResponse hres = hc.execute(hr);
            org.apache.http.HttpEntity entity = hres.getEntity();
            re_code = hres.getStatusLine().statusCode;
            if (re_code == 200) {
            //your successCode here
            String repMsg = org.apache.http.util.EntityUtils.toString(entity,"GBK");
            }else{
            //your failCode here
            }
            
  最后,根据实际需要,解析返回报文,决定是否去关闭连接 

我资源里有免费下载地址 http://download.csdn.net/detail/tsytdhs163/4241756

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值