Java 如何知道我是否已成功连接到打开连接的URL?

Java 如何知道我是否已成功连接到打开连接的URL?,java,url,jakarta-ee,networking,connection,Java,Url,Jakarta Ee,Networking,Connection,若要检查是否可以连接到名为“服务”的web应用,请打开其地址的连接。如何检查是否已连接到此URL/服务?我正在考虑发送某种状态方法(当客户机尝试打开与此URL的连接时),该方法确定客户机是否能够成功建立与服务的连接。但是我该怎么做呢 final URL url = new URL("http://localhost:8084/service/index.jsp"); final URLConnection urlc = url.openConnection

若要检查是否可以连接到名为“服务”的web应用,请打开其地址的连接。如何检查是否已连接到此URL/服务?我正在考虑发送某种状态方法(当客户机尝试打开与此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 ?
我试着这样做:

          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 ?
我建议

          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 ?
    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
(好)

我建议

          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 ?
    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
(OK)

使用url.getResponseCode()==OK(200)检查web应用程序是否返回OK状态如何?@Waqas它是什么方法?我确信它不是一个预定义的方法..?它在HttpUrlConnection类中:使用url.getResponseCode()==OK(200)检查web应用程序是否返回OK状态如何?@Waqas它是什么方法?我确信它不是预定义的方法..?它在HttpUrlConnection类中:
          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 ?