【发布时间】:2014-09-08 12:42:51
【问题描述】:
我有以下几行java代码:
d = Jsoup
.connect(getUrl)
.timeout(5000)
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0")
.followRedirects(true)
.get();
在实现 Runnable 并由 Thread Executor 启动的线程类中。 我的问题是,Jsoup 不断触发异常是因为:
org.jsoup.HttpStatusException: HTTP error fetching URL. Status=502, URL=http://sub.domain.de:8080/ws/Codes/Texts-Listen;Stud-Sets;name;AAFF-B?template=UNEinzelGru&weeks=39&days=&periods=3-64&Width=0&Height=0
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
at com.noncomercial.parsingthread.Threads.VParserThread.run(VThread.java:62)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
如果我尝试在浏览器中访问 URL,一切正常。 如果我尝试一个示例 jsoup 连接,例如:
Document d = Jsoup.connect("http://sub.domain.de:8080/ws/Codes/Texts-Listen;Stud-Sets;name;AAFF-B?template=UNEinzelGru&weeks=39&days=&periods=3-64&Width=0&Height=0").get();
它也不会触发异常。我真的不知道我的线程或连接有什么问题。
任何想法:-/?
//编辑: 好吧,这很有趣: 当我开始在此连接上使用断点调试我的代码时,它神奇地不再发生了。
所以我想到了服务器端同时存在多个连接的问题......
我将我的 ExecutorService 设置为: executor = Executors.newFixedThreadPool(1);所以一次只使用一个线程。它从 10 分钟开始运行并且没有发生错误...
好的,我认为要解析的页面超过 500 个,基于线程的解决方案会很棒,有什么想法可以摆脱这些错误吗?
【问题讨论】:
标签: java http connection jsoup