HttpUrlConnection.openConnection does not work a second time

I know this problem should be fixed with System.setProperty ("http.keepAlive", "false"); before openConnection, but that didn't help me. First try this code, the second is unsuccessful. Even if I try this request in less than 5 seconds, it also works. If I wait anymore, this will not work again

This is my code:

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()); 

I get an exception in the last line:

 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) 

Does anyone have an idea of ​​what's wrong here? Thank!

+31
Jul 28 2018-10-10T00:
source share
6 answers

I solved the problem. Here I leave you a code if this can be useful to someone. Basically, I see a tendency for Google to use HttpClient / HttpGet instead of HttpUrlConnection. So I tried with these classes and it worked:

 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()); 
+3
Jul 29 '10 at 6:43
source

The connection pool used by HttpURLConnection while maintaining network connections is broken, so it tries to use the connections that were closed by the server. By default, Android sets KeepAlive for all connections.

System.setProperty("http.keepAlive", "false"); is a workaround that disables KeepAlive for all connections, so you avoid errors in the connection pool.

conn.setRequestProperty("Connection","Keep-Alive"); enables KeepAlive for this particular connection, substantially changing what System.setProperty("http.keepAlive", "false"); does System.setProperty("http.keepAlive", "false"); .

Also, I always explicitly call connect() , as it makes it clear where you are ending the connection setup. I'm not sure if calling this method is optional or not.

 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()); 
+28
Sep 13 '10 at 20:19
source

You do not need System.setProperty("http.keepAlive", "false");

All you need is conn.setRequestProperty("connection", "close");

this fixes the problem, but effectively kills while preserving the rights and, therefore, potentially makes multiple connections slower (which is a shame). I looked at the tracking tracker, but could not find anything.

@fonetik, did you know that this has already been achieved with harmony? I do not mean that this helps, since another http related luni defect is still not assigned after more than a month.

+22
Oct. 15 '10 at 15:45
source

this error was fixed in Android2.3, because we know that System.setProperty("http.keepAlive", "false"); not a good solution, because on a mobile device, each connection is associated with a high cost each time.

+1
May 28 '11 at 15:56
source

I believe your problem is in the order of your code. Check these methods in JavaDocs URLConnection - setRequestProperty should not be called after the connection is made on mUrl.openConnection (). It may work for the first time because the connection is completed, and then you change the settings, which do not affect anything, until the next connection attempt. Try using the HttpURLConnection constructor instead, so that you can call connect () after you set the properties.

0
Jul 29 '10 at 5:40
source

When I try to open an https connection, it works fine, but the second time it fails because I set the value of the system property instead of the HttpsURLConnection connection. I have java.io.IOException error: Write: I / O issue when opening https connection a second time. I used the following code in my application.

 System.setProperty("http.proxyHost", proxy); System.setProperty("http.proxyPort", port); 

But when I changed the same below, it works fine.

 javax.net.ssl.HttpsURLConnection ucon = (javax.net.ssl.HttpsURLConnection) urlWPF.openConnection(proxyserver); ucon.setRequestProperty("http.proxyHost", proxy); ucon.setRequestProperty("http.proxyPort", port); 

If you set the system property, it applies to the entire application. If you want reset to match, you can follow two paths. Firstly, you need to update the server, and secondly, you need to change the HttpsURLConnection.setRequestProperty , which is indicated above, where necessary.

0
Mar 12 '14 at 17:38
source



All Articles