SSLPeerUnverifiedException OkHttp?

后端 未结 3 1740

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:05

    Check SSLSession hostname and your connection host name...

    OkHttpClient client = new OkHttpClient();
    client.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
                        System.out.println("Warning: URL host '" + urlHostName
                                + "' is different to SSLSession host '"
                                + session.getPeerHost() + "'.");
                    }
                    return true;
                }
            });
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-03 05:09

    UPDATE

    Code for com.squareup.okhttp3:okhttp:3.0.1

    mTextView = (TextView) findViewById(R.id.textView);
    mHandler = new Handler(Looper.getMainLooper());
    
    final Request request = new Request.Builder()
            .url("https://...")
            .post(formBody)
            .build();
    
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, final IOException e) {
            Log.e(LOG_TAG, e.toString());
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    String message = request.toString() + "\r\n" + e.toString();
                    mTextView.setText(message);
                }
            });
        }
    
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try {
                JSONObject jsonObject = new JSONObject(response.body().string());
                final String message = jsonObject.toString(5);
                Log.i(LOG_TAG, message);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mTextView.setText(message);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    

    Because your project uses OkHttp v3.0.0-RC1, so to fix that Exception, your code should be as the following sample:

            OkHttpClient client = new OkHttpClient.Builder()
                    .hostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    })
                    .build();
    
            Request request = new Request.Builder()
                    .url("https://...")
                    .build();
    
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(final Request request, final IOException e) {
                    // do something...
                }
    
                @Override
                public void onResponse(Response response) throws IOException {
                    // do something...
                }
            });
    

    However, instead of return true; above, I suggest that you read Google's documentation about Common Problems with Hostname Verification for more information.

    One more useful link is OkHttp's HTTPS wiki.

    Hope it helps!

    P/S: please note that I use async way of OkHttp (at client.newCall(request).enqueue(new Callback()...), you can also use sync way as your code.

    0 讨论(0)
提交回复
热议问题