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?
I'm doing project using struts1. I'm fetching RSS feeds using ROME but it fails for two conditions:
To avoid such conditions what should I do?
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";
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.