0
存在しないホスト/サーバーに接続しようとすると、私のプログラムはちょうど死にそうです。デバッガをステップ実行すると何も貸されません。それはgetResponseCode()
になり、動作を停止します。私が言うことができ、プログラムが返ってこないものから例外は投げられません。存在しないサーバーへのjava.net.HttpURLConnectionの処理
try {
//construct a URL and open the connection
URL url = new URL("http://" + serverHost + ":" + serverPort + urlSuffix);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
if(http.getResponseCode() != 200) {
System.out.println("Could not connect");
}
System.out.println("Connected");
return;
} catch (MalformedURLException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
} catch (Exception e) { //give me something please
e.printStackTrace();
return;
}
有効なURLに接続し、それが正常に動作します:
はここで、関連するコードスニペットです。
いずれの場合でも入力ストリームを必ず読み込んでください(特にエラー) –