Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSLPeerUnverifiedException OkHttp?

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:

    public String okHttpRequest() throws IOException{

        OkHttpClient client = new OkHttpClient();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {

                return true;
            }
        });
        HttpUrl.Builder urlBuilder = HttpUrl.parse("myurl").newBuilder();
        urlBuilder.addQueryParameter("username","username");
        urlBuilder.addQueryParameter("password","7777");
        String url = urlBuilder.build().toString();

        Request request = new Request.Builder()
                .url(url)
                .build();

        //HERE EXCEPTION IS THROWN
        Response response = client.newCall(request).execute(); 
        return response.body().string();
}

The exception is:

javax.net.ssl.SSLPeerUnverifiedException: Hostname {domain} not verified:

like image 663
Alvin Avatar asked Sep 25 '22 17:09

Alvin


1 Answers

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.

like image 64
BNK Avatar answered Nov 15 '22 09:11

BNK