Я пытался получить канал rss через java, и я все время получаю ошибку 403. Я искал вокруг и, по-видимому, из-за пустых user-agent варов.
Это то, что я пробовал до сих пор:
try {
url = new URL("http://*****.com/feed/");
InputStream is = null;
try {
URLConnection con = url.openConnection();
con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
con.connect();
is = con.getInputStream();
feed = FeedParser.parse(con.getURL());
} catch (IOException e) {
System.out.println("error");
try
{
throw e;
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
if( is != null)
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FeedIOException e) {
e.printStackTrace();
} catch (FeedXMLParseException e) {
e.printStackTrace();
} catch (UnsupportedFeedException e) {
e.printStackTrace();
}
int items = feed.getItemCount();
for (int i = 1; i <= items; i++) {
FeedItem item = feed.getItem(i-1);
System.out.println(i+" Title: " + item.getTitle());
}
У меня проблемы с работой, я уверен, что я не делаю это правильно. Библиотека, которую я использую для анализа RSS-канала, - feed4j.
Заранее спасибо.
Feed4j не поддерживает настройку свойств запроса. Поэтому вы не можете этого сделать, если вы не измените класс FeedParser на что-то вроде этого
public static Feed parse(URL url, String userAgent) throws IOException, FeedIOException, FeedXMLParseException, UnsupportedFeedException {
try {
URLConnection con = url.openConnection();
if (userAgent != null) {
con.addRequestProperty("User-Agent", userAgent);
}
con.connect();
InputStream is = con.getInputStream();
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(is);
int code = FeedRecognizer.recognizeFeed(document);
switch (code) {
case FeedRecognizer.RSS_1_0:
return TypeRSS_1_0.feed(url, document);
case FeedRecognizer.RSS_2_0:
return TypeRSS_2_0.feed(url, document);
case FeedRecognizer.ATOM_0_3:
return TypeAtom_0_3.feed(url, document);
case FeedRecognizer.ATOM_1_0:
return TypeAtom_1_0.feed(url, document);
default:
throw new UnsupportedFeedException();
}
} catch (DocumentException e) {
throw new FeedXMLParseException(e);
}
}
Также в github