要检查我是否可以连接到名为 service 的网络应用程序,请打开与其地址的连接。如何检查是否已连接到此网址/服务 ?我正在考虑发送某种状态方法(当客户端尝试打开与此URL的连接时),该方法确定客户端是否能够建立与服务的成功连接。但是我该怎么做?
我正在尝试这样的事情:
final URL url = new URL("http://localhost:8084/service/index.jsp");
final URLConnection urlc = url.openConnection();
InputStream response = urlc.getInputStream();
// Now call a method in the service app that sends a status method...
// .....or can I get away with this ?
答案 0 :(得分:4)
我建议
URL url = new URL("http://stackoverflow.com/questions/13775103/how-do-i-know-if-i-have-successfully-connected-to-the-url-i-opened-a-connection");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
System.out.println(conn.getResponseCode());
打印200
(确定)