HttpURLConnection with POST "timeout" on ICS, works in HC

Android Android application does not work with ICS due to Login problem. When I looked at my code and ran it in debug mode on the ICS tablet, I see a problem, but I do not understand it. The code works correctly on all Honeycomb models that I tested, and in fact I have two tablets connected to my computer (one Samsung Galaxy Tab 3.2 tab, as well as Motorola Xoom wifi 4.0.3), and the code does not work on ICS and works on HC.

The error is a Socket timeout exception. The timeout was 2000 ms, but I increased it to 100000 ms for testing, and this did not affect.

Using the browser on the ICS tablet, I can go to the URL and it responds, so it is not connected to the network.

I am starting a background thread using AsyncTask.

Slurp simply takes all the input from an InputStream and uses StringBuilder to create a string representation. This is actually not useful in this request, but I added it to see what the server will respond.

I submit to the page just as the user authenticates using the form, so I use x-www-form-urlencoded.

Again, this code works fine on Honeycomb, but does not work on ICS.

The code makes a connection, but does not work when it requests a response from the server, much like the server is still waiting for something ... anyway, here is the code:

static public String authenticate(String service_url, String username, String password) throws IOException { if (username == null || password == null) throw new IOException(); String charset = "UTF-8"; String query = String.format("Email=%s&Password=%s",URLEncoder.encode(username, charset),URLEncoder.encode(password, charset)); byte [] data = query.getBytes(charset); URL url = new URL(service_url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Length", Integer.toString(data.length)); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setReadTimeout(5000); // 2 second timeout. try { connection.connect(); DataOutputStream pw = new DataOutputStream (connection.getOutputStream()); pw.writeBytes(query); pw.flush(); pw.close(); int code = connection.getResponseCode(); //SOCKET TIMEOUT HERE if (code == 200 || code == 302) { InputStream is = connection.getInputStream(); String value = slurp(is); List<String> cookies = connection.getHeaderFields().get("Set-Cookie"); if (cookies == null) throw new IOException(); for (String cookie : cookies) { if (cookie.startsWith("cpms")) { cookieTime = new DateTime(); //crazy but the expires time in the cookie is not actually accurate. return cookie; // this is the only correct path out. } } } else Logger.e(StaticUtils.class, "Invalid response code while logging in: " + code); } catch (IOException ioe) { Logger.e(StaticUtils.class, ioe); throw ioe; // log it and then throw it back. } finally { connection.disconnect(); } return null; } 
+4
source share

Source: https://habr.com/ru/post/1395426/


All Articles