在Android应用程序中从http转换为https后出现SocketTimeoutException [英] SocketTimeoutException after converting from http to https in android app

查看:197
本文介绍了在Android应用程序中从http转换为https后出现SocketTimeoutException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试将我的Android应用程序转换为使用SSL在我的Android应用程序和Web服务器之间传输信息后,我遇到了一些问题。 (SocketTimeOutException)

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)

我从证书颁发机构(CA)购买了一个肯定的SSL证书,并将我的服务器配置为正确使用它。我已经在我的网络浏览器中测试了它并且它正常工作。

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.

现在我正在尝试修改我的Android应用程序以使用https而不是http,但这是第一次我自己使用过https,对于我需要在java代码中实现的步骤感到有点困惑。

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.

目前我正在设置我配置的设置页面我的应用程序使用正确的URL。例如,我在活动中有2个文本字段,我输入网站URL(http://www.mydomain.com)和相关页面存储在的应用程序文件夹(myappfolder)

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)

然后我使用以下代码连接并测试连接变量是否正确配置,其中validateurl.aspx是一个网页,如果页面存在则返回JSON字符串:

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;
}

现在我尝试转换(http://www.mydomain.com) )变量使用https我得到上面提到的java.net.SocketTimeoutException。

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

我已经google了这个问题,看到我应该在上面实现HttpsURLConnection而不是URLConnection代码,所以我相应修改了我的代码如下:

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;
}

但是我仍然收到java.net.SocketTimeoutException。任何想法我做错了什么?

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

推荐答案

好的,我发现导致SocketTimeOutException的问题。

Ok i found the issue that was causing the SocketTimeOutException.

问题似乎是我在家工作,而手机正在使用服务器所在的同一网络。在这种情况下,似乎无法使用https访问服务器。我关闭了手机无线网络并使用手机3G连接进行连接,嘿嘿连接并验证了我的网址。

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.

我在测试https连接方面遇到了类似的问题在Web浏览器中从服务器和我的工作笔记本电脑。在每种情况下,我通过将www.mydomain.com地址添加到主机文件中来解决问题。

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.

我现在要研究编辑我的android手机主机文件看看这是否会解决它,因为我不想使用手机G3连接进行测试,因为我预计会收到很多费用...

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

这篇关于在Android应用程序中从http转换为https后出现SocketTimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆