1. 程式人生 > >How to ignore SSL certificate errors in Apache HttpClient 4.4

How to ignore SSL certificate errors in Apache HttpClient 4.4

public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        HttpClientBuilder b = HttpClientBuilder.create();

        // setup a Trust Strategy that allows all certificates.
        //
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();
        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);
        b.setConnectionManager( connMgr);

        // finally, build the HttpClient;
        //      -- done!
        CloseableHttpClient client = b.build();
        return client;
    }

相關推薦

How to ignore SSL certificate errors in Apache HttpClient 4.4

public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { Ht

How to train your Neural Networks in parallel with Keras and Apache Spark

Apache Spark on IBM Watson StudioNow, we will finally train our Keras model using the experimental Keras2DML API. To be able to execute the following code,

[Python] How to unpack and pack collection in Python?

ide ont add off art video lec ref show It is a pity that i can not add the video here. As a result, i offer the link as below: How to

How To View the HTML Source in Google Chrome

inner eve spi together member mes mnt line split Whether you are new to the web industry or a seasoned veteran, viewing the HTML source o

How to Find Processlist Thread id in gdb !!!!!GDB 使用

ren openss lua comm lte ext htm out int https://mysqlentomologist.blogspot.jp/2017/07/ Saturday, July

How to Install The Latest Eclipse in Ubuntu 16.04, 15.10?

How to Install The Latest Eclipse in Ubuntu 16.04, 15.10? 1. Install Java Don’t have Java installed? Search for and install OpenJDK Java 7 or

How To Change Log Rate Limiting In Linux

ratelimit record cap reac systemctl evel rem mat mil By default in Linux there are a few different mechanisms in place that may rate limi

How to setup oAuth 1.0 in NetSuite RESTlet API 如何在NetSuite中設定RESTlet API的oAuth認證

步驟如下: 1. Got Restlet URL 訪問RESTlet的Deployment,這樣獲取WebService要Post或訪問到的具體URL地址, 如果你疑惑RESTlet是什麼,那要等我下一篇文章再介紹。   2. Setup Roles for Token user, goe

[轉]How to display the data read in DataReceived event handler of serialport

本文轉自:https://stackoverflow.com/questions/11590945/how-to-display-the-data-read-in-datareceived-event-handler-of-serialport   問: I have the followin

How to use *args and **kwargs in Python

這篇文章寫的滿好的耶,結論: 1星= array, 2星=dictionary. 1星範例: def test_var_args(farg, *args): print "formal arg:", farg for arg in args: print "an

How to know the directory size in CENTOS 檢視資料夾大小

Under any linux system, you want to use the command du. (Disk Usage) Common usage is : du -sh file(s) name(s) or du -sh /path/to/dir/* du -sh

How to display count of notifications in app launcher icon

Android (“vanilla” android without custom launchers and touch interfaces) does not allow changing of the application icon, because it is sealed in the .ap

How to get client Ip Address in Java Servlet

Try this one, String ipAddress = request.getHeader("X-FORWARDED-FOR"); if (ipAddress == null) { ipAddress = request.getRemoteAdd

[iOS] How to make a Global function in Swift

You can create a custom class with the method you need like this: class MyScene: SKScene { func CheckMusicMute() { if InGameMusicOnOff == tr

Ask HN: How to take advantage of living in the Bay?

Before moving to the Bay, I had hoped that the Bay to America would be like America to the rest of the world.I grew up in a developing country with an auth

How to write tidy SQL queries in R

How to write tidy SQL queries in RMost of us have to interact with databases nowadays, and SQL is by far the most common language used. However, working wi

How to make the impossible possible in CSS with a little creativity

CSS Previous sibling selectors don’t exist, but that doesn’t mean we can’t use themIf you ever used CSS sibling selectors, you know there’s only two. The +

Ask HN: How to validate a market opportunity in a cost effective way?

Here's the way that I do it. Copied from a previous comment:Keywords, Google keyword tool, forum chats, Reddit comments. Use tools like these to find EVIDE

Ask HN: How to model numerical energy data in Wolfram Alpha

I'm working on a dataset that contains energy supply and consumption data. This is just a hobby and idea is to do visualizations and simple moodelling base

How to Plan an Engineering Project in 5 Easy Steps

Let’s start with a disclaimer: I am not a project planning expert. I’m not a certified scrum master or even a very organized person. I’m just a guy who’s b