即使设置了连接超时,HTTPS请求也只会在低于20的Android API上挂起

我做了一个java.net.HttpURLConnection ,并挂在线connection.connect()即使我已经设置连接超时。 “b4 connect”被logging,“连接后”永远不会被logging。 我已经在API 21及以上版本上进行过testing,但是我在API 16-19的testing中遇到了这个问题。 这是我的代码如下。 该请求使用https,后端使用标准的nginx httpsconfiguration。

  URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); try { connection.setRequestMethod("GET"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"); connection.setConnectTimeout('\uea60'); connection.setReadTimeout('\uea60'); connection.setInstanceFollowRedirects(false); Log.d(TAG, "b4 connect"); connection.connect(); Log.d(TAG, "after connect"); if(connection.getResponseCode() == 200) { return IOUtils.toString(connection.getInputStream(), "UTF-8"); } } catch (Exception var) { throw new Exception(var.getMessage()); } finally { connection.disconnect(); } return null; 

Solutions Collecting From Web of "即使设置了连接超时,HTTPS请求也只会在低于20的Android API上挂起"