url.openStream 有害吗?

发布于 2024-09-04 16:48:57 字数 785 浏览 7 评论 0原文

我使用 java.net.URL.openStream() 方法从服务器检索内容。我最近遇到一个问题,HTTP 响应代码指示错误,但仍然读取流而不是抛出异常。这导致错误在执行过程中很晚才出现,并被证明是一个转移注意力的错误。据我所知,当您使用此方法打开流时,无法检查 HTTP 响应代码。

我能找到正确处理此问题的唯一方法是在打开流之前获取连接:

HttpURLConnection conn=(HttpURLConnection) url.openConnection()
#Code updated with scotth's suggestion
if(!String.valueOf(conn.getResponseCode()).startsWith('2'))
    throw new IOException("Incorrect response code "+conn.getResponseCode()+" Message: " +getResponseMessage());
rawIn=conn.getInputStream()

InputStream in=conn.getInputStream()

那么您同意吗?是否存在安全使用 openStream 的良好环境,或者是否应该阻止其使用。值得注意的是,Sun 在其 教程代码中使用了该方法直接从 URL 读取。不过,代码会抛出异常,因此它并不是良好编码实践的堡垒。

I was using the java.net.URL.openStream() method to retrieve content from the server. I recently ran into an issue where the HTTP Response code indicated an error, but instead of throwing an exception, the stream still was read anyway. This caused the error to appear much later in the execution and proved to be a red herring. As far as I can see, when you have opened a stream using this method, there is no way to check the HTTP response code.

The only way I could find to handle this properly was to obtain a connection before opening the stream:

HttpURLConnection conn=(HttpURLConnection) url.openConnection()
#Code updated with scotth's suggestion
if(!String.valueOf(conn.getResponseCode()).startsWith('2'))
    throw new IOException("Incorrect response code "+conn.getResponseCode()+" Message: " +getResponseMessage());
rawIn=conn.getInputStream()

InputStream in=conn.getInputStream()

So do you agree? Are there any good circumstances for using openStream safely, or should its use be discouraged. It is worth noting that Sun uses the method in their tutorial code for reading directly from a URL. Then again, the code throws Exception so it isn't exactly a bastion of good coding practices.

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

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

发布评论

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

评论(1

注定孤独终老 2024-09-11 16:48:57

如果您希望您的类免受 url 类型变化的影响,例如在绝对文件路径 (file:///)、jar-contained 之间进行更改,openStream() 就可以正常工作资源,以及潜在的其他协议,甚至可能带有自定义协议处理程序(scotth://foo.bar)。

然而,正如您发现的,它的抽象性相当高,因此,如果您想了解有关与资源交互的性质的任何详细信息,您将需要 openConnection() 并按您的要求进行转换看看合适。

回复:其他状态代码 - 您可能想浏览 RFC2616 - 如果您关心的只是“成功”,您只需检查 String.valueOf(conn.getResponseCode()).startsWith('2') 即可。

openStream() works just fine if you want your class to be shielded from changes in the type of url - to change between, for example, absolute file paths (file:///), jar-contained resources, and potentially other protocols maybe even with custom protocol handlers (scotth://foo.bar).

However, as you've found its abstraction is quite high, so if you desire to know any details whatsoever about the nature of the interaction with the resource you'll need to openConnection() and cast as you see fit.

Re: other status codes - you probably want to glance at RFC2616 - if all you care about is "successful" you can just check that String.valueOf(conn.getResponseCode()).startsWith('2').

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文