我正在尝试使用OkHttp库将带有一些url参数的ojit_rpost 请求发送到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();
}

异常(exception)是:
javax.net.ssl.SSLPeerUnverifiedException: Hostname {domain} not verified:

最佳答案

更新
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()...),也可以将同步方式用作代码)。

关于android - SSLPeerUnverifiedException OkHttp吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34671926/

10-12 01:52