java.net.URL 如何打开连接?它使用套接字吗?

发布于 2024-10-20 12:49:44 字数 564 浏览 1 评论 0原文

我使用以下代码从 URL 获取 JSON 字符串:

public static String getStringFromURL(String addr) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    URL url = new URL(addr);
    org.apache.commons.io.IOUtils.copy(url.openStream(), output);
    return output.toString();
}

我想确保如果“addr”处的页面因任何原因失败,这不会挂起。我不希望它使我们的服务器瘫痪或发生任何事情。我们开始研究 java.net.URL 如何打开连接,但无法从 Javadoc(我们使用的是 1.5)。任何想法或内部知识将不胜感激。如果你能引用来源,那就更好了。谢谢!

I'm using the following code to get a JSON string from a URL:

public static String getStringFromURL(String addr) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    URL url = new URL(addr);
    org.apache.commons.io.IOUtils.copy(url.openStream(), output);
    return output.toString();
}

I want to make sure this doesn't hang if the page at "addr" fails for any reason. I don't want it to bring our server down or anything. We started looking into how java.net.URL opens the connection and couldn't tell much from the Javadoc (we are using 1.5). Any thoughts or inside knowledge would be appreciated. If you can cite sources, so much the better. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑依然 2024-10-27 12:49:44

从技术上讲,这取决于协议。对于 HTTP,它使用 TCP/IP 套接字。如果发生 I/O 错误,openStream() 将引发异常。只需将其放入 try/catch 中即可。但是,如果服务器返回例如 HTTP 404(未找到)或 500(内部错误),您将在不知不觉中将此明文放入字符串中。您可能需要使用 HttpURLConnection 来进行更细粒度的控制。

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

if (connection.getResponseStatus() == 200) {
    // All OK, convert connection.getInputStream() to string.
    // Don't forget to take character encoding into account!
} else {
    // Possible server error. Throw exception yourself? Or return some default?
}

此外,您可以设置超时 <代码>URLConnection#setConnectTimeout()。我相信,它默认为 3 秒左右。您可能需要对其进行调整以使其更快。设置 1000 1 秒。

Technically, that depends on the protocol. For HTTP, it uses TCP/IP sockets. The openStream() will throw an exception if an I/O error occurs. Just put it in a try/catch. However, if the server returns for example a HTTP 404 (not found) or 500 (internal error), you will get this plain into the string unawarely. You may want to use HttpURLConnection instead for more fine-grained control.

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

if (connection.getResponseStatus() == 200) {
    // All OK, convert connection.getInputStream() to string.
    // Don't forget to take character encoding into account!
} else {
    // Possible server error. Throw exception yourself? Or return some default?
}

Further you can set the timeout URLConnection#setConnectTimeout(). I believe, it defaults to 3 seconds or something. You may want to tweak it to make it all faster. Set with 1000 for 1 second.

深爱不及久伴 2024-10-27 12:49:44

是的,它会挂起。

有两个超时需要考虑:

  • 连接超时:服务器可能既不接受 (ACK) 也不拒绝 (RST) 连接,因为它受防火墙保护。这相当短,可以使用 setConnectTimeout();
  • 连接等待数据的超时时间。这个过程相当长(5 分钟),并且通常会失败,例如,如果 Web 应用程序正在等待来自池的数据库连接。可以使用 setReadTimeout( )

Yes, it will hang.

There are two timeouts to consider:

  • The connection timeout: The server may neither accept (ACK) nor reject (RST) the connection because it is firewalled. This is rather short and can be set using setConnectTimeout();
  • The timeout the connection waits for data. This one is rather long (5 minutes) and is the usual thing that fails, for example if the webapplication is waiting for a database connection from a pool. It can be set using setReadTimeout()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文