使用 Https 调用 https 接口

最近公司让用 Java 调用一个国外的接口,打开文档一看 要使用 https 调用 https 接口。弄了几天终于弄的差不多了。

首先配置本地单向 https

1.生成安全证书(个人学习可以使用工具生成,而如果是公司的则另当别论了)
这个网上一大堆教程

2.配置tomcat 服务器

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"  
               maxThreads="150" scheme="https" secure="true"  
               clientAuth="false" sslProtocol="TLS"  
               keystoreFile="F:\tomcat.keystore"  
               keystorePass="tomcat"  
               ciphers="tomcat"  
               /> 

手动复制,端口号可修改

3.修改 web.xml 配置文件

<security-constraint>    
        <!-- Authorization setting for SSL -->    
        <web-resource-collection >    
            <web-resource-name >SSL</web-resource-name>    
            <url-pattern>/*</url-pattern>    
        </web-resource-collection>    
        <user-data-constraint>    
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>    
        </user-data-constraint>    
 </security-constraint>  

上面的是针对所有请求转化的,有具体需要自行百度

完成上面的部分以后就可以使用 https 访问你自己的 action , jsp 什么的了。


调用 https 第三方接口
  1. 配置 SSLClient ,让它继承 DefaultHttpClient,具体代码如下:
 public SSLClient() throws Exception{  
        super();  
        SSLContext ctx = SSLContext.getInstance("TLS");  
        X509TrustManager tm = new X509TrustManager() {  
                @Override  
                public void checkClientTrusted(X509Certificate[] chain,  
                        String authType) throws CertificateException {  
                }  
                @Override  
                public void checkServerTrusted(X509Certificate[] chain,  
                        String authType) throws CertificateException {  
                }  
                @Override  
                public X509Certificate[] getAcceptedIssuers() {  
                    return null;  
                }  
        };  
        ctx.init(null, new TrustManager[]{tm}, null);  
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
        ClientConnectionManager ccm = this.getConnectionManager();  
        SchemeRegistry sr = ccm.getSchemeRegistry();  
        sr.register(new Scheme("https", 443, ssf));  
    }  

然后我们就可以愉快的使用 https 了,举一个例子,我们使用 httpClient 进行文件和其它数据的上传

//传递图片到 April 官网上 左边为文件名 ,后边为文件路径
    public static String upPicToApril(String filename , String file ){
        String url = "******";//url
         //1:创建一个httpclient对象
       HttpClient httpclient = null;
       HttpPost httppost = null ;

       Charset charset = Charset.forName("UTF-8");//设置编码
       String author = "Basic " + Base64.encode(("username:password").getBytes());
       String resString = null ;
       try {
        try {
                httpclient = new SSLClient();
                httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,600000);
                httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,900000);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           //2:创建http的发送方式对象,是GET还是post
        httppost = new HttpPost(url);

        httppost.setHeader("Authorization", author);
        httppost.setHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)");
           //3:创建要发送的实体,就是key-value的这种结构,借助于这个类,可以实现文件和参数同时上传,很简单的。
           MultipartEntity reqEntity = new MultipartEntity();

           //图片位置
           FileBody bin = new FileBody(new File(file));
           StringBody comment = new StringBody(filename,charset);

           reqEntity.addPart("image", bin);
           reqEntity.addPart("filename", comment);
           httppost.setEntity(reqEntity);

           //4:执行httppost对象,从而获得信息
           HttpResponse response1 = httpclient.execute(httppost);
           HttpEntity resEntity = response1.getEntity();

           //获得返回来的信息,转化为字符串string
           resString = EntityUtils.toString(resEntity);

       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       } catch (IllegalStateException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
       }
    return resString;
    }

完工。。。。。。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值