资讯专栏INFORMATION COLUMN

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

Tony_Zby / 3416人阅读

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

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设置

</>复制代码

  1. import java.security.KeyManagementException;
  2. import java.security.KeyStoreException;
  3. import java.security.NoSuchAlgorithmException;
  4. import java.security.cert.CertificateException;
  5. import java.security.cert.X509Certificate;
  6. import javax.net.ssl.SSLContext;
  7. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  8. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  9. import org.apache.http.conn.ssl.TrustStrategy;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import org.apache.http.ssl.SSLContextBuilder;
  13. public class HttpClientTest
  14. {
  15. public static CloseableHttpClient createSSLClient()
  16. throws KeyManagementException, NoSuchAlgorithmException,
  17. KeyStoreException
  18. {
  19. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
  20. null, new TrustStrategy()
  21. {
  22. public boolean isTrusted( X509Certificate[] chain,
  23. String authType ) throws CertificateException
  24. {
  25. return true;
  26. }
  27. } ).build();
  28. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  29. sslContext, NoopHostnameVerifier.INSTANCE );
  30. return HttpClients.custom().setSSLSocketFactory( sslsf ).build();
  31. }
  32. }

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

</>复制代码

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.URI;
  6. import java.net.URISyntaxException;
  7. import java.security.KeyManagementException;
  8. import java.security.KeyStoreException;
  9. import java.security.NoSuchAlgorithmException;
  10. import org.apache.commons.logging.Log;
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.ClientProtocolException;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.methods.HttpGet;
  16. import org.apache.http.impl.client.CloseableHttpClient;
  17. import org.apache.http.util.EntityUtils;
  18. public class HttpClientUtil
  19. {
  20. public static void main( String[] args ) throws ClientProtocolException,
  21. IOException, URISyntaxException, KeyManagementException,
  22. NoSuchAlgorithmException, KeyStoreException
  23. {
  24. String url = "https://localhost:8443/maven-example/hello";
  25. CloseableHttpClient httpClient = HttpClientTest.createSSLClient();
  26. HttpGet get = new HttpGet();
  27. get.setURI( new URI( url ) );
  28. HttpResponse response = httpClient.execute( get );
  29. String s = streamToString( response.getEntity().getContent() );
  30. System.out.println( s );
  31. }
  32. private static String streamToString( InputStream is )
  33. throws IOException
  34. {
  35. String line = "";
  36. StringBuilder total = new StringBuilder();
  37. BufferedReader rd = new BufferedReader( new InputStreamReader( is ) );
  38. while ( (line = rd.readLine()) != null )
  39. {
  40. total.append( line );
  41. }
  42. return total.toString();
  43. }
  44. }

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

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


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

</>复制代码

  1. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  2. sslContext);
  3. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  4. 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元查看
<