如何避免罗马例外
我正在使用struts1做项目。 我正在使用 ROME 获取 RSS 提要,但由于以下两种情况而失败:
- 当我的防火墙禁止 rss url 时(响应代码 403)
- 当我插入不正确的 rss url 时
为了避免出现此类情况,我应该做什么?
I'm doing project using struts1.
I'm fetching RSS feeds using ROME but it fails for two conditions:
- When my firewall forbidden rss url (response code 403)
- When I insert incorrect rss url
To avoid such conditions what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需捕获异常并处理它们即可。
Just catch the exceptions and handle them.
有些情况是您无法避免的。
您无法避免网络中断,也无法避免输入错误的 URL。
不过,您可以检查网络是否可达,以及 URL 输入是否正确。
您应该捕获异常并向用户提供有意义的错误消息。
There are some situations you simply cannot avoid.
You can't avoid network outages, you can't avoid incorrectly typed URLs.
What you can do, however, is check if network is reachable, and whether the URL is typed correctly or not.
You should catch the exceptions and provide meaningful error messages to the user.
关于403
有些源似乎有一些保护(针对 DDOS)
因此,根据用户代理(在您的情况下为“Java”),他们拒绝您阅读提要
因此,在打开这样的连接之前,您必须设置自己的用户代理(如 Firefox 用户代理),
我当前的 USER_AGENT 字符串是
“Mozilla/5.0(Windows NT 10.0;WOW64;rv:41.0)Gecko/20100101 Firefox/41.0”;
About 403
Some feeds seems have some protection (for DDOS)
So based on user Agent (in your case "Java") they deny you to read the feed
So you have to set your own user agent (like the firefox user agent), before opening connection like this
My current USER_AGENT String is
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0";