javax.net.ssl.SSLHandshakeException?

时间:2018-11-21 10:21:06

标签: java android ssl ca

我正在尝试使用HTTPS连接(UrlHttpsConnection类)连接到WCF服务器,并始​​终收到错误“找不到证书路径的信任锚”。 我在网上找到了数千个有关该问题的示例,但没有什么对我有真正的帮助。

我的WCF服务使用由内部CA签名的证书,该证书已添加到我的智能手机上的受信任CA列表中。如果我在智能手机上通过Chrome调用了https://myserver/myservice/test网址,则不再发出警告,该证书被视为有效。从我的应用程序中,我不断收到错误消息。

您知道为什么我的应用程序不认为服务器证书有效,而Chrome浏览器却有效吗?我该如何解决?

出于安全原因,我不想忽略SSL验证。

预先感谢您的建议。

1 个答案:

答案 0 :(得分:0)

尝试这种方式,但是我使用了api调用的改进。.

  public class ApiClient {
//public final static String BASE_URL = "https://prod.appowiz.com/app/services/";
public final static String BASE_URL_SECURE = "Pass your url";

public static ApiClient apiClient;

private Retrofit retrofit = null;
private static Retrofit storeRetrofit = null;

public Retrofit getClient(Context context) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL_SECURE)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();


    return retrofit;
}
 public static Retrofit getStore() {
    if (storeRetrofit == null) {
        final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {

            }


            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {

            }


            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {

                return new java.security.cert.X509Certificate[0];
            }
        }};

        // Install the all-trusting trust manager
        final SSLContext sslContext;
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        try {
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .sslSocketFactory(sslSocketFactory).hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
                    .build();
            storeRetrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL_SECURE)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .build();


        } catch (NoSuchAlgorithmException | KeyManagementException e1) {
            CustomLogHandler.printErrorlog(e1);
        }

    }

    return storeRetrofit;
}

用于api调用创建界面。

public interface ApiInterface {

@POST("device/add_device_name")
Call<AddDeviceNameVo> addDeviceName(@Body JsonObject body);

 }

像这样将api称为活动或片段。

        apiInterface = ApiClient.getStore().create(ApiInterface.class);