SSLPeerUnverifiedException OkHttp?

时间:2016-01-08 07:47:43

标签: android ssl okhttp

我正在尝试使用OkHttp库通过一些url参数向API发送发布请求。关注this blog post到目前为止我有这个代码:

    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();
}

例外是:

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

3 个答案:

答案 0 :(得分:4)

<强>更新

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();
        }
    }
});

由于您的项目使用 OkHttp v3.0.0-RC1 ,因此要修复该异常,您的代码应如下所示:

        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...
            }
        });

但是,我建议您阅读Google有关Common Problems with Hostname Verification的文档,而不是上面的return true;,以获取更多信息。

另一个有用的链接是OkHttp's HTTPS wiki

希望它有所帮助!

P / S:请注意我使用OkHttp的异步方式(在client.newCall(request).enqueue(new Callback()...),你也可以使用同步方式作为你的代码。

答案 1 :(得分:0)

显然,您尝试连接到SSL网站(https),因此您需要在小代码段下方添加SSLSocketFactory

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

有关详细信息,请参阅此pagethis,它可以为您提供帮助。

如果您想“信任所有证书”,请参阅this示例,但建议,并且只应用于测试目的!< / p>

答案 2 :(得分:0)

检查SSLSession主机名和您的连接主机名......

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;
            }
        });