简体   繁体   English

如何避免罗马例外

[英]How to avoid rome exception

I'm doing project using struts1. 我正在使用struts1进行项目。 I'm fetching RSS feeds using ROME but it fails for two conditions: 我正在使用ROME获取RSS feed,但由于以下两种情况而失败:

  1. When my firewall forbidden rss url (response code 403) 当我的防火墙禁止RSS网址时(响应代码403)
  2. When I insert incorrect rss url 当我插入错误的rss网址时

To avoid such conditions what should I do? 为了避免这种情况,我该怎么办?

About 403 约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 某些提要似乎具有某种保护(对于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);

My current USER_AGENT String is 我当前的USER_AGENT字符串是
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0"; “ Mozilla / 5.0(Windows NT 10.0; WOW64; rv:41.0)Gecko / 20100101 Firefox / 41.0”;

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

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. 但是,您可以做的是检查网络是否可以访问以及URL的键入是否正确。

You should catch the exceptions and provide meaningful error messages to the user. 您应该捕获异常并向用户提供有意义的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM