简体   繁体   English

在 Android/Java 中发送 HTTPS Post 请求

[英]Sending HTTPS Post Request in Android/Java

since sslSocketFactory(javax.net.ssl.SSLSocketFactory)' is deprecated, what should I use instead?由于 sslSocketFactory(javax.net.ssl.SSLSocketFactory)' 已弃用,我应该改用什么? Thanks in regards... Deprecated.谢谢... 已弃用。 SSLSocketFactory does not expose its X509TrustManager, which is a field that OkHttp needs to build a clean certificate chain. SSLSocketFactory 不会暴露其 X509TrustManager,这是 OkHttp 构建干净证书链所需的字段。 This method instead must use reflection to extract the trust manager.此方法必须使用反射来提取信任管理器。 Applications should prefer to call sslSocketFactory(SSLSocketFactory, X509TrustManager), which avoids such reflection.应用程序应该更喜欢调用 sslSocketFactory(SSLSocketFactory, X509TrustManager),这样可以避免这种反射。 Sets the socket factory used to secure HTTPS connections.设置用于保护 HTTPS 连接的套接字工厂。 If unset, the system default will be used.如果未设置,将使用系统默认值。

try {
                        client = new OkHttpClient().newBuilder()
                                .sslSocketFactory(trustCert().getSocketFactory())
                                .build();
                    } catch (CertificateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (KeyStoreException e) {
                        e.printStackTrace();
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    } catch (KeyManagementException e) {
                        e.printStackTrace();
                    }
                    MediaType mediaType = MediaType.parse("text/plain");
                    RequestBody body = new FormBody.Builder()
                            .build();
    
                    Request request = new Request.Builder()
                            .url("https://example.com")
                            .method("GET", null)
                            .addHeader("auth-token", token)
                            .addHeader("Authorization", "Bearer"+" "+token)
                            .build();
                    Response response = null;
                    try {
                        response = client.newCall(request).execute();
                        odgovor = response.body().string();
                        System.out.println(odgovor);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

      private SSLContext trustCert() throws CertificateException,IOException, KeyStoreException,
                NoSuchAlgorithmException, KeyManagementException {
            AssetManager assetManager = getAssets();
            InputStream is = getApplicationContext().getResources().openRawResource(R.raw.gdig2);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            Certificate ca = cf.generateCertificate(is);
    
            // Create a KeyStore containing our trusted CAs
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
    
            // Create a TrustManager that trusts the CAs in our KeyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);
    
            // Create an SSLContext that uses our TrustManager
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, tmf.getTrustManagers(), null);
            return context;
        }

Use, #sslSocketFactory(SSLSocketFactory, X509TrustManager)使用,#sslSocketFactory(SSLSocketFactory, X509TrustManager)

client = new okHttpClient().newBuilder().sslSocketFactory(trustCert().getSocketFactory(), (X509TrustManager) tmf.trustManagers.get(0));客户端 = 新 okHttpClient().newBuilder().sslSocketFactory(trustCert().getSocketFactory(), (X509TrustManager) tmf.trustManagers.get(0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM