如何避免罗马例外

发布于 2025-01-04 18:13:10 字数 163 浏览 5 评论 0原文

我正在使用struts1做项目。 我正在使用 ROME 获取 RSS 提要,但由于以下两种情况而失败:

  1. 当我的防火墙禁止 rss url 时(响应代码 403)
  2. 当我插入不正确的 rss url 时

为了避免出现此类情况,我应该做什么?

I'm doing project using struts1.
I'm fetching RSS feeds using ROME but it fails for two conditions:

  1. When my firewall forbidden rss url (response code 403)
  2. When I insert incorrect rss url

To avoid such conditions what should I do?

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

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

发布评论

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

评论(3

帝王念 2025-01-11 18:13:10

只需捕获异常并处理它们即可。

Just catch the exceptions and handle them.

梦年海沫深 2025-01-11 18:13:10

有些情况是您无法避免的。

您无法避免网络中断,也无法避免输入错误的 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.

沦落红尘 2025-01-11 18:13:10

关于403
有些源似乎有一些保护(针对 DDOS)
因此,根据用户代理(在您的情况下为“Java”),他们拒绝您阅读提要
因此,在打开这样的连接之前,您必须设置自己的用户代理(如 Firefox 用户代理),

 System.setProperty("http.agent", USER_AGENT);
 URLConnection openConnection = url.openConnection();
 is = url.openConnection().getInputStream();
 if ("gzip".equals(openConnection.getContentEncoding())) {
     is = new GZIPInputStream(is);
 }
 InputSource source = new InputSource(is);
 input = new SyndFeedInput();
 syndicationFeed = input.build(source);
 XmlReader reader = new XmlReader(url);
 syndicationFeed = input.build(reader);

我当前的 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

 System.setProperty("http.agent", USER_AGENT);
 URLConnection openConnection = url.openConnection();
 is = url.openConnection().getInputStream();
 if ("gzip".equals(openConnection.getContentEncoding())) {
     is = new GZIPInputStream(is);
 }
 InputSource source = new InputSource(is);
 input = new SyndFeedInput();
 syndicationFeed = input.build(source);
 XmlReader reader = new XmlReader(url);
 syndicationFeed = input.build(reader);

My current USER_AGENT String is
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0";

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