为什么重置连接?

时间:2018-08-04 21:04:54

标签: java jsoup

我正在尝试使用JSoup连接到URL。对于一个网址,它工作正常,但对于另一个网址,我就得到了

java.net.SocketException: Connection reset

这是有效的代码:

Document doc = Jsoup.connect("https://finance.yahoo.com/quote/IBM/key-statistics?p=IBM").get();

以下是生成错误的代码:

Document doc = Jsoup.connect("https://www.nasdaq.com/dividend-stocks/dividend-calendar.aspx?date=2018-Aug-17").get();

如果您将这些网址剪切并粘贴到浏览器中,则它们可以正常工作。有建议吗?

1 个答案:

答案 0 :(得分:2)

在下面的评论中感谢Pshemo,这是答案:

    String address = "https://www.nasdaq.com/dividend-stocks/dividend-calendar.aspx?date=" +date;
    //Note: "userAgent required for this site to prevent java.net.SocketException: Connection reset" error
    String usrAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
    Document doc = Jsoup.connect(address).userAgent(usrAgent).get();