我正在使用以下代码检查互联网连接:
try {
HttpURLConnection httpConnection = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204").openConnection());
httpConnection.setRequestProperty("User-Agent", "Test");
httpConnection.setRequestProperty("Connection", "close");
httpConnection.setConnectTimeout(15000);
httpConnection.connect();
if (httpConnection.getResponseCode() == 204 && httpConnection.getContentLength() == 0){
//internet is avialable
return;
}else{
Log.e(TAG, "Internet connection error: " + httpConnection.getResponseCode()
+ ": " + httpConnection.getResponseMessage());
}
} catch (IOException e) {
Log.e(TAG, "Internet connection error: " + e);
}
我收到以下响应:代码:204 消息:无内容
但内容长度大于 0,因此失败。有人可以帮我理解发生了什么吗?
谢谢,阳光