我创建了一个java.net.HttpURLConnection
,它挂在了connection.connect()
行,即使我设置了连接超时。 “b4 connect”被记录,“连接后”永远不会被记录。我已经在API 21及更高版本上进行了测试,并且工作正常,但是我在API 16-19上的测试中遇到了这个问题。这是我的代码如下。请求使用https,后端使用标准的nginx https配置。
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;
答案 0 :(得分:6)
您使用某些unicode字符来指定时间。请尝试这样的常规数字:
connection.setConnectTimeout(30000);
сonnection.setReadTimeout(30000);
请记住"连接后#34;不会在超时时记录。将抛出异常。