我使用Java HttpURLConnection
连接我的服务器:
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) urlObj.openConnection();
} catch (IOException e) {
logger.severe("ConnectionImpl - request(): IOException e: "+e.getMessage());
e.printStackTrace();
}
connection.setDoInput(true);
connection.setRequestMethod("GET");
try {
connection.connect();
} catch (IOException e) {
logger.severe("ConnectionImpl - request(): IOException e: "+e.getMessage());
e.printStackTrace();
}
int statusCode = connection.getResponseCode();
我如何测试我的应用是否可以与https连接,如果失败则会失败回到http?
答案 0 :(得分:0)
您可以通过检查状态代码来执行此操作,如下所示:
int statusCode = connection.getResponseCode();
if(statusCode==200){
Log.d("Connection Status","Can connect");
}
else{
Log.d("Connection Status","Can't connect");
}
或
if(statusCode == HttpURLConnection.HTTP_OK){
Log.d("Connection Status","Can connect");
}
else{
Log.d("Connection Status","Can't connect");
}