httpclient调用https webservice(rest) 忽略证书

最近做一个项目需要调用rest风格的webservice,但对方提供一个https的url,所以需要忽略证书去访问。httpclient4.3的api相比3.x改动挺大的,所以,把自己实现的分享出来。代码有些乱,大家参考下。有些方法没贴出来。

  1. 这个地方需要重生isTrusted方法,达到忽略本地证书的目的。

    private  SSLConnectionSocketFactory buildSSLContext()
             throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
         SSLContext sslcontext = SSLContexts.custom().setSecureRandom(new SecureRandom()).loadTrustMaterial(null, new TrustStrategy() {
                     public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                         return true;
                     }
                 }).build();
         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
         return sslsf;
     }

2. 这部分是具体调用webservice,功能是实现上传一个文件。

CloseableHttpClient httpclient = null;
  HttpPost httppost = null;
  try {
   // create upload file
   uploadFile = createUploaFile(user, appCenterUrl, buildTypeId);
   //  get upload url
   String uploadUrl = getUploadUrl(userMqa, mqaUrl);
   // get httpclient
   SSLConnectionSocketFactory sslsf = buildSSLContext();
   httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
   httppost = new HttpPost(uploadUrl);
   httppost.addHeader(HttpHeaders.AUTHORIZATION, gererateEncoudAuth(userMqa));
   FileBody bin = new FileBody(uploadFile);
   StringBody comment = new StringBody("A binary file of some kind", ContentType.MULTIPART_FORM_DATA);
   HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("resource", bin).addPart("comment", comment).build();
   httppost.setEntity(reqEntity);
   System.out.println("executing request " + httppost.getRequestLine());
   
   CloseableHttpResponse httpResponse = httpclient.execute(httppost);
   try {
    System.out.println(httpResponse.getStatusLine().getStatusCode());
   
    HttpEntity resEntity = httpResponse.getEntity();
        
    if (resEntity != null) {
     boolean flag = true;
     System.out.println("Response content length: " + resEntity.getContentLength());
     String resultStr = EntityUtils.toString(resEntity);
    }
   } finally {
    if (httpResponse != null) {
     httpResponse.close();
    }
   }
  } catch (Exception e) {
    e.printStackTrace();

} finally {
   try {
    if (httppost != null) {
     httppost.releaseConnection();
    }
    if (httpclient != null) {
     httpclient.close();
   }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

时间: 2024-10-12 18:11:31

httpclient调用https webservice(rest) 忽略证书的相关文章

java 通过httpclient调用https 的webapi

java如何通过httpclient 调用采用https方式的webapi?如何验证证书.示例:https://devdata.osisoft.com/p...需要通过httpclient调用该接口,没有做过https 方式的调用不知道怎么解决. java 通过httpclient调用https 的webapi >> csharp 这个答案描述的挺清楚的:http://www.goodpm.net/postreply/csharp/1010000008927916/java通过httpclie

关于httpclient 请求https (如何绕过证书验证)

第一种方法,适用于httpclient4.X 里边有get和post两种方法供你发送请求使用.导入证书发送请求的在这里就不说了,网上到处都是 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.n

[转]java 关于httpclient 请求https (如何绕过证书验证)

原文:http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两种方法供你发送请求使用. 导入证书发送请求的在这里就不说了,网上到处都是 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamRea

Java调用https接口,避免证书的方式

一.使用httpClient调用 1.使用maven添加依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>org.apach

HttpClient调用.net发布的带Windows NTML验证的webservice

使用HttpClient调用: 先要通过Windows NTML验证,然后才能调用 . 1 class WebServiceTest 2 { 3 4 /** 5 * 获取接口数据 6 * @param soapRequest 7 * @return String 8 */ 9 public String postSoapRequest(String soapRequest){ 10 CloseableHttpClient httpclient = HttpClients.createDefaul

HTTPClient 发送HTTPS请求

HTTPClient 发送HTTP请求就不多说了, 现在给出发送HTTPS请求, 主要思路是忽略证书验证. /** * * @param url * @param contextType "image/jpeg","application/Json" * @return */ public static byte[] sendHttpsGetUrl(HttpClient httpClient1 ,String url,String contextType) { //

[译]HttpClient请求HTTPS URL

1.概览 本文将演示如何配置Apache HttpClient 4 添加ssl支持.目的很简单----无需有效证书即可成功请求 HTTPS URLs. 如果你想深入挖掘和学习其他和HttpClient相关的酷知识,请点击httpclient-guide 延伸阅读: httpclient-connection-management httpclient-advanced-config httpclient-4-cookies 2. SSLPeerUnverifiedException异常 使用ht

C#远程调用技术WebService葵花宝典

一.课程介绍 直接开门见山吧,在学习之前阿笨想问大家一句,关于WebService远程过程调用技术(RPC) 你真的会了吗?不要跟老夫扯什么WebService技术已经过时,如果你的内心有在偷偷告诉你其实我是真的不会WebService的话,那么恭喜你,因为你在这茫茫的IT编程世界里找到了这本<C#远程调用技术WebService葵花宝典>!曾经有一位不知名的讲师说过这么一句名言: 一门RPC技术不会,那么千万万门RPC技术将都不会! 本次阿笨的分享课包含以下知识点. 1.C# WebServ

Java调用https服务报错unable to find valid certification path to requested target的解决方法

我们网站要进行https改造,配置上购买的SSL证书后,浏览器访问正常,但是写了个java代码用httpcomponents调用https rest接口时报错: Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath