Rome XmlReader not reading https feed

回眸只為那壹抹淺笑 提交于 2019-12-04 16:48:52

When I hit that URL from my browser it asks for basic authentication. You can do this with ROME:

URL feedUrl = new URL(feed)
HttpURLConnection httpcon = (HttpURLConnection)feedUrl.openConnection();
String encoding = new sun.misc.BASE64Encoder().encode("username:password".getBytes());
httpcon.setRequestProperty  ("Authorization", "Basic " + encoding);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(httpcon));

You probably shouldn't use sun.misc.BASE64Encoder. Rather find another one somewhere.

From: http://cephas.net/blog/2005/02/09/retrieving-an-rss-feed-protected-by-basic-authentication-using-rome/

I find this a bit more elastic when it comes to authentication, this code works with and without authentication:

URL feedUrl = new URL("http://the.url.to/the/feed");
//URL feedUrl = new URL("http://user:pass@the.url.to/the/feed");

HttpURLConnection connection = (HttpURLConnection) feedUrl.openConnection();
if (feedUrl.getUserInfo() != null) {
    String encoding = new sun.misc.BASE64Encoder().encode(feedUrl.getUserInfo().getBytes());
    connection.setRequestProperty("Authorization", "Basic " + encoding);
}

SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(connection));

You could also use the following in place of

String encoding = new sun.misc.BASE64Encoder().encode("username:password".getBytes());

to this:

String BASIC_AUTH = "Basic " + Base64.encodeToString("username:password".getBytes(), Base64.NO_WRAP);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!