【问题标题】:SocketTimeoutException after converting from http to https in android app在android应用程序中从http转换为https后的SocketTimeoutException
【发布时间】:2012-07-05 09:56:44
【问题描述】:

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

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

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

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

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

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。

我用谷歌搜索了这个问题,发现我应该在上面的代码中实现 HttpsURLConnection 而不是 URLConnection,所以我根据以下内容修改了我的代码:

    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。任何想法我做错了什么?

【问题讨论】:

    标签: android ssl https ca socket-timeout-exception


    【解决方案1】:

    好的,我找到了导致 SocketTimeOutException 的问题。

    问题似乎是我在家工作,而手机使用的网络与服务器所在的网络相同。在这种情况下,它似乎无法使用 https 访问服务器。我关闭了手机的无线网络并使用手机的 3G 连接进行连接,然后应用程序已连接并验证了我的 URL。

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

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

    稍后我会在下面发表评论,说明进展如何

    我希望这可以帮助其他有类似问题的人

    【讨论】:

    • 在使用以下链接 androidauthority.com/how-to-root-your-samsung-galaxy-ace-37385 对我的手机进行生根后,然后从 google play 下载并使用应用程序 Hosts Editor 即可。我能够编辑我的主机文件并添加我的网络服务器的 ip 和别名。这很有效,现在我可以在内部网络上使用 https 连接到网络服务器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多