如何测试我的应用程序是否可以连接到 https 并在其他情况下回退到 http?
How to test if my app can connect with https and fallback to http otherwise?
我使用 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?
您可以通过检查状态代码来做到这一点,如下所示:
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");
}
我使用 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?
您可以通过检查状态代码来做到这一点,如下所示:
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");
}