0

Please guide me to find the problem in this code, when i use url with HTTP then it works fine but with HTTPS it's giving problem.I have googled but couldn't find a solution. It would be appreciable if someone guide me to save my time Thanks

public String doRequest() {

    String serverUrl = Constants.BASE_URL + mAction;
    Log.d("usm_serverUrl",serverUrl);
    if (mParams != null) {
        serverUrl += "?" + mParams;
    }
    Log.d("usm_serverUrl",serverUrl);
    try {
        URL url = new URL(serverUrl);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", "application/json");
        conn.setRequestMethod("GET");
        if(conn.getResponseCode() == HttpsURLConnection.HTTP_OK){
            // Do normal input or output stream reading
            Log.d("usm_requestCode","Successful");
        }
        else {
            Log.d("usm_requestCode","Failed");
           // response = "FAILED"; // See documentation for more info on response handling
        }
        //HttpURLConnection conn = (HttpURLConnection) url.openConnection();


        InputStream in = new BufferedInputStream(conn.getInputStream());
        Log.d("debug", "get-url: " + serverUrl);
        String response = IOUtils.toString(in, "UTF-8");
        Log.d("debug", "get-url: " + serverUrl + "\n post-response: " + response);
        return response;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

I'm getting the following exception:

11-30 22:43:30.553    5031-5398/com.dhcollator.routetoschool W/System.err﹕ javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:107)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
11-30 22:43:30.554    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
11-30 22:43:30.555    5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
Usman Rana
  • 2,067
  • 1
  • 21
  • 32

2 Answers2

1

For https url connection to work in android, use HttpsUrlConnection instead of HttpUrlConnection. See Documentation here

Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • Thanks but 'm still getting the problem . The problem is with the ssl certificate, can you please guide that from where to get the file for the following code that need to replaced with "load-der.crt" CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt")); – Usman Rana Nov 30 '15 at 18:03
  • can you share your server url. – Deepak Goyal Nov 30 '15 at 18:07
0

refer link, it will help as HttpsUrlConnection is required instead of HttpUrlConnection. http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html

http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

Deepak
  • 756
  • 4
  • 10