2

I found similar post on How to post HTTPS request using Retrofit? but it does't help me that much so i was wondering if some of you could help. This is my first post on SO and if you need any more information please let me know.

This is my code

public static OkHttpClient getUnsafeOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

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

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[0];
            }
        } };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts,
                new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext
                .getSocketFactory();

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        okHttpClient.networkInterceptors().add(new Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder()
                        .header("user-agent", "Android")
                        .header("Basic","apikey")
                        .build();
                return chain.proceed(request);
            }
        });

I know i can access https on my server with browser but when i try to do it here like

   public WebServiceCaller(OnServiceFinished listener) {
    final String baseUrl = "https://myserver.com/";
    this.listener=listener;

    final OkHttpClient client = getUnsafeOkHttpClient();

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

    serviceCaller = retrofit.create(APIinterface.class);
}

then it can not finish the request. As soon as i move s from https it works. So i guess my question is why is it not working and how can i fix this?

this are my dependencies

compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.7.0'
  • Please include the fail/success logs from the okhttp client - you will need to add a debug interceptor that logs the traffic. – dazza5000 Jan 18 '18 at 13:51
  • Can you check this: https://github.com/square/retrofit/issues/1037 – Chetak Bhimani Jan 18 '18 at 14:02
  • Maybe it's obvious, but I should ask: does your server have HTTPS enabled? If so, does it have a 3rd party certificate or is self signed? And if it's self signed, why not use https://letsencrypt.org/ certificates? Trusting all the certificates to be able to use a self signed certificate is (almost) like using HTTP in the first place (or worst, as it gives a false sense of security). – Xavier Rubio Jansana Jan 18 '18 at 14:09
  • To dazza5000 I am having a hard time adding debug interceptor :(. Xavier i am not sure if i am answering this right but here it goes. I put my php script on a server that is not mine. This is all for my school project so false sense of security is not the issue. I can access my script through https when using browser but id does not work in code. I will now go and see letsencrypt.org – Mihovil Maricic Jan 18 '18 at 14:24
  • I have found the answer here : https://stackoverflow.com/questions/46807237/protocolexception-expected-status-header-not-present – Mihovil Maricic Jan 18 '18 at 16:13

0 Answers0