Jsoup seems to be caching, can I disable this?

2019-08-07 05:06发布

I'm trying to use reddit.com/r/subreddit/random to get a random link from /r/nocontext. However, it seems to bring me to the same link repeatedly should I put this code in a loop. This brings me to think that Jsoup is caching the webpage, but I need to disable this. Here's my code:

Document doc = null;
try {
    doc = Jsoup.connect("http://www.reddit.com/r/nocontext/random").get();
    Elements elements = doc.select("div.entry.unvoted p.title a.title");

    for (Element link : elements) 
        System.out.println(link.text());

} catch (IOException e) {
    e.printStackTrace();
}

Anyone have any insight on how to fix this problem?

2条回答
一纸荒年 Trace。
2楼-- · 2019-08-07 05:17

It is most probably cached by the server. check the Rules in their API.

especially I quote this:

Most pages are cached for 30 seconds, so you won't get fresh data if you request the same page that often. Don't hit the same page more than once per 30 seconds.

so make your request be every 30+ seconds and see if your problem is solved.

查看更多
做个烂人
3楼-- · 2019-08-07 05:17

GET requests can be cached, where as POST requests can never be cached. Changing your connection request from get() to post() will avoid this.

查看更多
登录 后发表回答