【问题标题】:How do I know if I have successfully connected to the URL I opened a connection with?我如何知道我是否已成功连接到打开连接的 URL?
【发布时间】:2012-12-08 06:23:27
【问题描述】:

为了检查我是否可以连接到名为 service 的网络应用程序,我打开了与其地址的连接。如何检查是否已连接到此 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 ?

【问题讨论】:

标签: java url jakarta-ee networking connection


【解决方案1】:

我建议

    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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    相关资源
    最近更新 更多