HttpsURLConnection的间歇性故障同一网址

HttpsURLConnection的间歇性故障同一网址

本文介绍了HttpsURLConnection的间歇性故障同一网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我遇到了同样的
结果
这是Android 1.5的SDK。
我碰巧打电话低于code(这是一种方法)有几次
相同的URL,并间歇性失败。结果
当它失败了,没有异常,流是空故
readConnection失败,GETRESPONSE code返回-1。结果
全球禁用缓存,setDefaultUseCaches(假);

我想一定有某种URL连接对象池的地方。

我如何能解决这个任何想法?

  HttpURLConnection的连接= NULL;
    尝试{
        网址URL =新的URL(this.url);
        连接=(HttpURLConnection类)url.openConnection();
        connection.setRequestProperty(授权,基本+
        Base64编码coder.en codeString的(用户+:+密码));
        connection.setRequestProperty(用户代理的userAgent);
        connection.connect();        readConnection(connection.getInputStream());        connection.disconnect();
    }赶上(IOException异常前){
               reportException(例如,connection.getResponse code())
    }赶上(ParserException前){
               reportException(例如,connection.getResponse code())
    }


解决方案

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

  System.setProperty(http.keepAlive,假);

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

 连接=(HttpURLConnection类)url.openConnection();

祝你好运!

I think I'm experiencing the same ashttp://groups.google.com/group/android-developers/msg/9d37d64aad0ee357
This is Android 1.5 SDK.I happen to call several times below code(which is in a method) withthe same url and it fails intermittently.
When it fails, there is no exception, the stream is empty so thereadConnection fails, and getResponseCode returns -1.
Global caching is disabled, setDefaultUseCaches(false);

I suppose there must be some kind of url connection object pool somewhere.

Any idea on how can I workaround this?

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())
    }
解决方案

The way I fixed this issue was to add the below line...

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

...before my connection line...

connection = (HttpURLConnection) url.openConnection();

Good luck!

这篇关于HttpsURLConnection的间歇性故障同一网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:09