HttpUrlConnection.openConnection第二次失败

ggo*_*eze 32 android httpurlconnection

我知道这个问题应该用System.setProperty修复("http.keepAlive","false"); 在openConnection之前,但这对我不起作用.首先尝试这个代码工作,第二个失败.即使我在不​​到5秒钟后尝试此请求,它也可以.如果我等待更多,它会再次失败

这是我的代码:

    System.setProperty("http.keepAlive", "false");
  HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
  conn.setUseCaches(false); 
  conn.setRequestProperty("Connection","Keep-Alive"); 
  conn.setRequestProperty("User-Agent", useragent);
  conn.setConnectTimeout (30000) ; 
  conn.setDoOutput(true); 
        conn.setDoInput(true); 

  consumer.sign(conn);
  InputSource is = new InputSource(conn.getInputStream());
Run Code Online (Sandbox Code Playgroud)

我在最后一行得到了例外:

java.io.IOException: Write error: I/O error during system call, Broken pipe
W/System.err( 2164):  at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativewrite(Native Method)
W/System.err( 2164):  at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access$600(OpenSSLSocketImpl.java:55)
W/System.err( 2164):  at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:583)
W/System.err( 2164):  at java.io.OutputStream.write(OutputStream.java:82)
W/System.err( 2164):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1332)
W/System.err( 2164):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
W/System.err( 2164):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
W/System.err( 2164):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1153)
W/System.err( 2164):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:253)
Run Code Online (Sandbox Code Playgroud)

有人知道这里有什么问题吗?谢谢!

bar*_*ton 29

HttpURLConnection它保持连接活动时使用的连接池被破坏,以便它尝试使用已由服务器关闭的连接.默认情况下,Android会在所有连接上设置KeepAlive.

System.setProperty("http.keepAlive", "false"); 是一种解决方法,禁用所有连接的KeepAlive,以便您避免连接池中的错误.

conn.setRequestProperty("Connection","Keep-Alive");为这个特定的连接打开KeepAlive,基本上扭转了这System.setProperty("http.keepAlive", "false");一点.

此外,我总是明确地打电话connect(),因为它清楚地说明了您在何处结束连接设置.我不确定调用此方法是否可选.

System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setUseCaches(false); 
conn.setRequestProperty("User-Agent", useragent);
conn.setConnectTimeout(30000);
conn.setDoOutput(true); 
conn.setDoInput(true); 
consumer.sign(conn);

conn.connect();

InputSource is = new InputSource(conn.getInputStream());
Run Code Online (Sandbox Code Playgroud)


RaB*_*RaB 24

你不需要 System.setProperty("http.keepAlive", "false");

所有你需要的是 conn.setRequestProperty("connection", "close");

这解决了问题,但有效地杀死了alives,因此可能使多个连接变得更慢(这是一种耻辱).我正在通过和谐bug跟踪器,但无法找到任何东西.

@fonetik,你知道这是否已经和谐地提升了吗?我的意思并不是说它有多大帮助,因为在一个多月之后,另一个与http相关的luni缺陷仍未分配.


ggo*_*eze 3

我解决了这个问题。在这里,我给您留下了代码,以防它对某人有帮助。基本上我在 Google 上看到了使用 HttpClient/HttpGet 而不是 HttpUrlConnection 的趋势​​。所以我尝试了这些课程,一切正常:

final HttpClient client = new DefaultHttpClient();
final HttpGet conn = new HttpGet(mURL.toString());

OAuthConsumer consumer = mOAuthManager.getPostConsumer();
consumer.sign(conn);
HttpResponse response = client.execute(conn);
InputSource is = new InputSource(response.getEntity().getContent());
Run Code Online (Sandbox Code Playgroud)

  • 谷歌现在建议使用 HttpUrlConnection (从 Gingerbread 开始),因为其中一些错误已得到修复:http://android-developers.blogspot.com/2011/09/androids-http-clients.html (10认同)