Spring 集成 RSS 源 - 403 错误

标签 spring rss spring-integration

我正在尝试使用 Spring Integration 来轮询 RSS 提要,并且我使用 SO 用户提要 URL 作为示例。但是,当应用程序运行时,我收到以下错误:

Disconnected from the target VM, address: '127.0.0.1:58603', transport: 'socket'
2016-03-04 19:34:36.252 ERROR 2345 --- [ask-scheduler-4] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessagingException: Failed to retrieve feed at url 'http://stackoverflow.com/feeds/user/813852'; nested exception is com.rometools.fetcher.FetcherException: Authentication required for that resource. HTTP Response code was:403
    at org.springframework.integration.feed.inbound.FeedEntryMessageSource.getFeed(FeedEntryMessageSource.java:216)
    at org.springframework.integration.feed.inbound.FeedEntryMessageSource.populateEntryList(FeedEntryMessageSource.java:182)
    at org.springframework.integration.feed.inbound.FeedEntryMessageSource.doReceive(FeedEntryMessageSource.java:157)
    at org.springframework.integration.feed.inbound.FeedEntryMessageSource.receive(FeedEntryMessageSource.java:122)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:175)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:224)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:57)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:176)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:173)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:330)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:324)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.rometools.fetcher.FetcherException: Authentication required for that resource. HTTP Response code was:403
    at com.rometools.fetcher.impl.AbstractFeedFetcher.throwAuthenticationError(AbstractFeedFetcher.java:183)
    at com.rometools.fetcher.impl.AbstractFeedFetcher.handleErrorCodes(AbstractFeedFetcher.java:170)
    at com.rometools.fetcher.impl.HttpURLFeedFetcher.retrieveAndCacheFeed(HttpURLFeedFetcher.java:186)
    at com.rometools.fetcher.impl.HttpURLFeedFetcher.retrieveFeed(HttpURLFeedFetcher.java:140)
    at com.rometools.fetcher.impl.HttpURLFeedFetcher.retrieveFeed(HttpURLFeedFetcher.java:99)
    at org.springframework.integration.feed.inbound.FeedEntryMessageSource.getFeed(FeedEntryMessageSource.java:204)
    ... 22 more

URL 通过浏览器加载正常(即使在隐身模式下,因此错误消息可能具有误导性)。我下载了 RSS 阅读器,提要运行良好。

这就是我配置提要的方式:

<int-feed:inbound-channel-adapter id="stackOverflow"
                                      channel="log"
                                      url="http://stackoverflow.com/feeds/user/813852">
        <int:poller fixed-rate="10000" max-messages-per-poll="100" />
</int-feed:inbound-channel-adapter>
<int:logging-channel-adapter id="log" level="DEBUG" />

我还尝试了另一个 RSS 提要 URL,它成功了。有什么想法可能是错误的吗?

最佳答案

我刚刚运行了wireshark跟踪,看起来StackOverflow不喜欢java用户代理:

User-Agent: Java/1.8.0_66\r\n

...

<p>The owner of this website (stackoverflow.com) has banned your access based
on your browser's signature (27e797571e1923de-ua21).</p>\n

设置自定义用户代理可以绕过此限制:

public class CustomFeedFetcher extends HttpURLFeedFetcher {
    @Override
    public void setUserAgent(String s) {
        super.setUserAgent("AgentName");
    }
}

关于Spring 集成 RSS 源 - 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35803648/

相关文章:

java - @CompoundIndex 在 Spring Data MongoDB 中不起作用

java - 使用 spring 集成解压

java - Spring @SessionAttributes 和 "Cannot create a session after the response has been committed"

java - Spring Boot 2.3 中不打印自定义异常的 Stacktrace

php - 如何添加我想要的链接以使用 PHP Universal FeedParser 阅读

ios - 我不知道我的代码有什么问题(带有 MWFeedParser 的 rss 阅读器)

c - 我的程序似乎没有终止;为什么?

java - 将重命名的文件作为输入传递到出站适配器/网关

java - 如何在 spring mqtt 集成中停止重复订阅收到的保留消息

java - hibernate二级缓存错误(Spring 4,Hibernate 4)