SSLPeerUnverifiedException OkHttp?

后端 未结 3 1749

I\'m trying to use OkHttp library to send post request to API with some url parameters. Following this blog post I have this code so far:

           


        
3条回答
  •  醉酒成梦
    2021-01-03 05:07

    Apparently you try to connect to a SSL website (https), therefore you need to add a SSLSocketFactory, below little code snippet.

    OkHttpClient client = new OkHttpClient();
    client.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
    

    For more details see this page or this, it should help you.

    If you want to "trust all certificates", see this example but it`s not recommanded and should only be used for testing purposes!

提交回复
热议问题