1

I am getting some problems after trying to convert my android app to use SSL to transport information between my android app and web server. (SocketTimeOutException)

I have bought a Positive SSL certificate from a Certificate Authority (CA) and configured my server to work with it correctly. I have tested it in my web browser and its working correctly.

Now I am trying to modify my android app to use https instead of http but as this is the first time I have used https myself, I am a little confused as to what steps I need to implement in the java code.

Currently I am working on my settings page where I configure my app to use the correct url. For example I have 2 text fields on an activity where I enter the website url (http://www.mydomain.com) and the application folder that the relevant pages are stored in (myappfolder)

I then use the following code to connect and test that the connection variables are configured correctly where validateurl.aspx is a webpage that returns a JSON string if the page exists:

protected boolean validateConnectionSettings() {        

    String result = "";
    boolean validated = false;
    try {
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx");
        URL url = new URL(urlBuilder.toString());
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 
        url = uri.toURL();

        URLConnection urlConn = url.openConnection();
        BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));

        String inputLine;
        while((inputLine = in.readLine()) != null){
            result += inputLine;
        }                   
        in.close();

        if(result.equals("exists")) {
            validated = true;
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    }

    return validated;
}

Now when I try converting the (http://www.mydomain.com) variable to use https I get the above mentioned java.net.SocketTimeoutException.

I have googled this issue and seen that I should be implementing HttpsURLConnection instead of URLConnection in the above code so I have modified my code accordingly to the following:

    protected boolean validateConnectionSettings() {        

    String result = "";
    boolean validated = false;
    try {
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx");
        URL url = new URL(urlBuilder.toString());
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); 
        url = uri.toURL();

        // URLConnection urlConn = url.openConnection();
        HttpsURLConnection urlConn = (HttpsURLConnection)url.openConnection();
        BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));

        String inputLine;
        while((inputLine = in.readLine()) != null){
            result += inputLine;
        }                   
        in.close();

        if(result.equals("exists")) {
            validated = true;
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    }

    return validated;
}

However I still recieve the java.net.SocketTimeoutException. Any Ideas what Im doing wrong?

1 Answer 1

1

Ok i found the issue that was causing the SocketTimeOutException.

It seems that the problem was that I am working from my home and the phone is using the same network that the server is located on. It cannot seem to get to the server using https when this is the case. I turned the phones wireless network off and connected using the phones 3G connnection and hey presto the app connected and validated my URL.

I had similar problems with testing the https connection directly from the server and my work laptop in a web browser. In each case I fixed the issue by adding the www.mydomain.com address into the host file.

I'm now going to look into editing my android phones host file to see if this will solve it as I dont want to use the phones G3 connection for testing as I expect ill get lots of charges...

Ill post a comment below later to say how it goes

I hope this helps anyone else that has similar problems

Sign up to request clarification or add additional context in comments.

1 Comment

ok after rooting my phone using the following link androidauthority.com/how-to-root-your-samsung-galaxy-ace-37385 and then donwloading and using the app Hosts Editor from google play. I was able to edit my host file and add the ip and alias of my webserver. This worked a treat and now I can connect to the webserver using https on my internal network

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.