资讯专栏INFORMATION COLUMN

构建https协议的webService并使用httpClient接口访问

Tony_Zby / 3110人阅读

摘要:组件版本信息使用自带的命令生成文件命令将拷贝到目录下配置的目录文件,在配置文件中新增配置将工程添加进并启动,使用访问和链接。原理后续进一步研究

1.组件版本信息
apache-tomcat-7.0.75
JDK 1.8.0_91

2.使用jdk自带的keytool命令生成keystore文件test.keystore
命令:keytool -genkey -alias test123 -keypass test123 -keyalg RSA -keysize 1024 -keystore test.keystore -storepass test123

3.将test.keystore拷贝到apache-tomcat-7.0.75bin目录下

4.配置tomcat的conf目录server.xml文件,在配置文件中新增SSL配置


5.将webservice工程添加进tomcat并启动,使用postman访问http和https链接。http可以正常访问,https访问不了,由于客户端证书问题

6.新建类HttpClientTest,用于配置https相关SSL设置

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;  
import javax.net.ssl.SSLContext;   
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

public class HttpClientTest
{
    public static CloseableHttpClient createSSLClient()
            throws KeyManagementException, NoSuchAlgorithmException,
            KeyStoreException
    {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
                null, new TrustStrategy()
                {
                    public boolean isTrusted( X509Certificate[] chain,
                            String authType ) throws CertificateException
                    {
                        return true;
                    }
                } ).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext, NoopHostnameVerifier.INSTANCE );
        return HttpClients.custom().setSSLSocketFactory( sslsf ).build();
    }
}

7.新建类HttpClientUtil,用于测试https的get请求

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.logging.Log;
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.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil
{
    public static void main( String[] args ) throws ClientProtocolException,
            IOException, URISyntaxException, KeyManagementException,
            NoSuchAlgorithmException, KeyStoreException
    {
        String url = "https://localhost:8443/maven-example/hello";
        CloseableHttpClient httpClient = HttpClientTest.createSSLClient();
        HttpGet get = new HttpGet();
        get.setURI( new URI( url ) );
        HttpResponse response = httpClient.execute( get );
        String s = streamToString( response.getEntity().getContent() );
        System.out.println( s );
    }

    private static String streamToString( InputStream is )
            throws IOException
    {

        String line = "";
        StringBuilder total = new StringBuilder();
        BufferedReader rd = new BufferedReader( new InputStreamReader( is ) );
        while ( (line = rd.readLine()) != null )
        {
            total.append( line );
        }
        return total.toString();
    }
}

8.执行main方法,正确输出https的response响应

9.操作过程中遇到一个问题,报主机名验证错误


解决方法:将new SSLConnectionSocketFactory(sslContext)修改为new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)即可。原理后续进一步研究

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext, NoopHostnameVerifier.INSTANCE);

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66765.html

相关文章

  • WebService就是这么简单

    摘要:它使用方式,接收和响应外部系统的某种请求。回顾我们在学习基础网络编程章节已经知道了这么一个连接了。使用指定名称的命名空间。名词简单对象访问协议作为一个基于语言的协议用于有网上传输数据。以的根元素出现。代理这么一个概念就更加清晰了。 WebService介绍 首先我们来谈一下为什么需要学习webService这样的一个技术吧.... 问题一 如果我们的网站需要提供一个天气预报这样一个需求...

    SwordFly 评论0 收藏0
  • 使用commons httpclient请求https协议webservice

    摘要:使支持协议类,是自定义私有类请求类根据请求报文,请求服务地址获取响应报文请求报文请求地址字符集类型封装的服务器响应参数和返回报文正常响应。 使commons httpclient支持https协议类,是commons httpclient import java.io.IOException; import java.net.InetAddress; import java.n...

    VPointer 评论0 收藏0
  • 实现一个spring webservice服务端四:服务端、客户端以及httpclient调用spr

    摘要:执行结果如下中华田园犬测试我认为所有使用协议的,都能使用测试。下面是我写的测试代码旺财需要增加一个包测试结果返回值如下中华田园犬写法稍微有点麻烦的是,需要拼接请求参数,参数少的话还好,多的话就很烦不过这种方法不用生成一大堆客户端代码。 经过前段时间的学习,已经实现一个有返回值的spring-ws服务,那接下来,就要试试能不能通过不同方式的调用,要实现一下几种方式的测试: spring...

    oneasp 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<