关于用 HttpClient 访问 https的问题,来人啊!

caishengkai 2017-01-18 08:42:20
我用HttpClient 访问 http OK没问题
访问 https的网站就不行,我知道有安全认证什么的,然后我上网找 关于https连接的 采用 忽略证书还有其他等等方法,还是不行,一直报这两个错

有时候一刷新就报第一个错误,再刷新变成第二个。 我的jdk是1.8的 换成1.7也不行。有大神知道吗!! 解决了分可以再加的!!!
...全文
561 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
oO临时工Oo 2017-01-22
  • 打赏
  • 举报
回复
引用 4 楼 trocp 的回复:
可能是由于盗版系统中有部分windows组件被删除了。 可能默认安全策略不一样。也许与IE的版本有关系。 另外,通过浏览器,先访问一下此HTTPS的站点,再试试运行程序。
上面说法有误,不是IE版本,应该是控制面板中的Internet选项,其中设置的安全级别,可以调低安全级别试试。
oO临时工Oo 2017-01-22
  • 打赏
  • 举报
回复
可能是由于盗版系统中有部分windows组件被删除了。 可能默认安全策略不一样。也许与IE的版本有关系。 另外,通过浏览器,先访问一下此HTTPS的站点,再试试运行程序。
caishengkai 2017-01-22
  • 打赏
  • 举报
回复
引用 2 楼 windowsoahil 的回复:
抄现有代码吧,我们使用的就是抄来的代码 jdk1.8,要求HttpClient4以上
package com.pingan.ela.structure.ex;

import com.pingan.ela.structure.web.converter.Jackson2FormHttpMessageConverter;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

/**
 * Created by LIHAO845 on 2016-12-02.
 */
@Component
public class HttpsRestTemplate extends RestTemplate {

    public HttpsRestTemplate() {
        super(new HttpComponentsClientHttpRequestFactory(acceptsUntrustedCertsHttpClient()));
//        处理出参需要用String否则中文乱码
//        setMessageConverters(Collections.singletonList(converter));
    }

    private static CloseableHttpClient acceptsUntrustedCertsHttpClient() {
        HttpClientBuilder b = HttpClientBuilder.create();
        // setup a Trust Strategy that allows all certificates.
        //
        SSLContext sslContext;
        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, (i, j) -> true).build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        b.setSslcontext(sslContext);
        // don't check Hostnames, either.
        //      -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        // here's the special part:
        //      -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
        //      -- and create a Registry, to register it.
        //
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory)
                .build();
        // now, we create connection-manager using our Registry.
        //      -- allows multi-threaded use
        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        connMgr.setMaxTotal(200);
        connMgr.setDefaultMaxPerRoute(100);
        b.setConnectionManager(connMgr);
        // finally, build the HttpClient;
        //      -- done!
        return b.build();
    }

}
不行的,我试了网上很多现有的,都报这个错。最后我重装系统了,tm就可以了,我猜是原来系统是盗版系统引起的安全问题。
解开者 2017-01-19
  • 打赏
  • 举报
回复
抄现有代码吧,我们使用的就是抄来的代码 jdk1.8,要求HttpClient4以上
package com.pingan.ela.structure.ex;

import com.pingan.ela.structure.web.converter.Jackson2FormHttpMessageConverter;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

/**
 * Created by LIHAO845 on 2016-12-02.
 */
@Component
public class HttpsRestTemplate extends RestTemplate {

    public HttpsRestTemplate() {
        super(new HttpComponentsClientHttpRequestFactory(acceptsUntrustedCertsHttpClient()));
//        处理出参需要用String否则中文乱码
//        setMessageConverters(Collections.singletonList(converter));
    }

    private static CloseableHttpClient acceptsUntrustedCertsHttpClient() {
        HttpClientBuilder b = HttpClientBuilder.create();
        // setup a Trust Strategy that allows all certificates.
        //
        SSLContext sslContext;
        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, (i, j) -> true).build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        b.setSslcontext(sslContext);
        // don't check Hostnames, either.
        //      -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        // here's the special part:
        //      -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
        //      -- and create a Registry, to register it.
        //
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory)
                .build();
        // now, we create connection-manager using our Registry.
        //      -- allows multi-threaded use
        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        connMgr.setMaxTotal(200);
        connMgr.setDefaultMaxPerRoute(100);
        b.setConnectionManager(connMgr);
        // finally, build the HttpClient;
        //      -- done!
        return b.build();
    }

}
caishengkai 2017-01-18
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶顶顶!

51,412

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧