【问题标题】:HttpsURLConnection failing intermittently to the same URLHttpsURLConnection 间歇性地失败到相同的 URL
【发布时间】:2010-05-15 18:41:42
【问题描述】:

我想我正在经历同样的事情 http://groups.google.com/group/android-developers/msg/9d37d64aad0ee357
这是 Android 1.5 SDK。 我碰巧在下面的代码(在一个方法中)调用了几次 相同的网址,但间歇性失败。
当它失败时,没有例外,流是空的,所以 readConnection 失败,getResponseCode 返回 -1。
全局缓存被禁用,setDefaultUseCaches(false);

我想某处一定有某种 url 连接对象池。

知道如何解决这个问题吗?

HttpURLConnection connection = null;
    try {
        URL url = new URL(this.url);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization", "basic " +
        Base64Coder.encodeString(user + ":" + password));
        connection.setRequestProperty("User-Agent", userAgent);
        connection.connect();

        readConnection(connection.getInputStream());

        connection.disconnect();
    } catch (IOException ex) {
               reportException(ex, connection.getResponseCode())
    } catch (ParserException ex) {
               reportException(ex, connection.getResponseCode())
    } 

【问题讨论】:

    标签: android https get


    【解决方案1】:

    我解决此问题的方法是添加以下行...

    System.setProperty("http.keepAlive", "false");
    

    ...在我的连接线之前...

    connection = (HttpURLConnection) url.openConnection();
    

    祝你好运!

    【讨论】:

      【解决方案2】:

      如果 getResponseCode 返回 -1,则表示存在某种连接错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 2019-04-18
        • 2011-10-21
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        相关资源
        最近更新 更多