java - Okhttp javax.net.ssl.sslexception : connection closed by peer

标签 java android okhttp

我调用 api 进行密码支付:https://test-api.pin.net.au/1/cards/

我试了一下 postman,效果不错。

当我使用 okhttp 2.7.5 应用到代码 android 时

我的代码:

OkHttpClient httpClient = new OkHttpClient();

    RequestBody requestBody = new FormEncodingBuilder()
            .add("publishable_api_key", BuildConfig.PIN_PAYMENT_PUBLISHABLE_KEY)
            .add("number", scanResult.cardNumber)
            .add("expiry_month", scanResult.expiryMonth+"")
            .add("expiry_year", scanResult.expiryYear+"")
            .add("cvc", scanResult.cvv)
            .add("address_postcode", scanResult.postalCode)
            .add("name", name).build();


    Request request = new Request.Builder()
            .url(BuildConfig.BASE_URL_PIN_PAYMENT + Constants.API_CARD_PIN_PAYMENT)
            .post(requestBody)
            .build();

    httpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            view.hideLoading();
            view.showError(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            view.hideLoading();
            if (!response.isSuccessful()){
                String body = "Something went wrong.Please try again later.";
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    body = object.getString("error_description");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                view.showError(body);
            }else {
                view.hideLoading();
                Gson gson = new Gson();
                CardPinPaymentResponse paymentResponse = gson.fromJson(response.body().string() , CardPinPaymentResponse.class);
                view.getTokenCardSuccess(paymentResponse);
            }
        }

    });

但它不起作用并发出:javax.net.ssl.SSLException: Connection closed by peer

最佳答案

当您连接到 https 服务器时,OKhttp 需要知道要提供哪些 TLS 版本和密码套件。这是连接 https 的示例代码

    ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)  
    .tlsVersions(TlsVersion.TLS_1_2)
    .cipherSuites(
          CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
          CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
          CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
    .build();

OkHttpClient client = new OkHttpClient.Builder() 
    .connectionSpecs(Collections.singletonList(spec))
    .build();

祝你好运!!!

关于java - Okhttp javax.net.ssl.sslexception : connection closed by peer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43600835/

相关文章:

java - 如何获取 OkHttp3 重定向的 URL?

android - 在添加内容之前必须调用 requestedFeature() - 在 Fragment 中的 ActionBar 中实现 Spinner

android - 如何在 Interceptor Android 中获取 401 状态代码时更改 Activity

okhttp3 - 如何在okhttp中更改请求的超时时间

java - Mule ESB 在服务器上部署时不接受 SSL 证书,但在本地开发机器上运行良好

java - java中如何将字节转换为二进制?

java - String.replace() 无法正常工作?我究竟做错了什么?

java - KeyListener 仅在单击 Button 后开始监听

android - Gradle——继承产品 flavor

android - 带有 OkHttp 的 HTTP 版本