jsoup-提示java.net.SocketTimeoutException:Read timed out

使用Jsoup.connect(url).get()连接某网站时偶尔会出现

java.net.SocketTimeoutException:Read timed out异常。

原因是默认的Socket的延时比较短,而有些网站的响应速度比较慢,所以会发生超时的情况。

解决方法:

链接的时候设定超时时间即可。

doc = Jsoup.connect(url).timeout(5000).get();

5000表示延时时间设置为5s。

测试代码如下:

1,不设定timeout时:

package jsoupTest;  

import java.io.IOException;  

import org.jsoup.*;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;  

public class JsoupTest {
    public static  void main(String[] args) throws IOException{
    String url = "http://www.weather.com.cn/weather/101010400.shtml";
    long start = System.currentTimeMillis();
    Document doc=null;
    try{
        doc = Jsoup.connect(url).get();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        System.out.println("Time is:"+(System.currentTimeMillis()-start) + "ms");
    }
    Elements elem = doc.getElementsByTag("Title");
    System.out.println("Title is:" +elem.text());
    }
}  

有时发生超时:

java.net.SocketTimeoutException: Read timed out

2,设定了则一般不会超时

package jsoupTest;  

import java.io.IOException;  

import org.jsoup.*;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;  

public class JsoupTest {
    public static  void main(String[] args) throws IOException{
    String url = "http://www.weather.com.cn/weather/101010400.shtml";
    long start = System.currentTimeMillis();
    Document doc=null;
    try{
        doc = Jsoup.connect(url).timeout(5000).get();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        System.out.println("Time is:"+(System.currentTimeMillis()-start) + "ms");
    }
    Elements elem = doc.getElementsByTag("Title");
    System.out.println("Title is:" +elem.text());
    }
}  
时间: 2024-11-05 11:50:01

jsoup-提示java.net.SocketTimeoutException:Read timed out的相关文章

java.net.SocketTimeoutException: Read timed out

If you get java.net.SocketTimeoutException: Read timed out exception Try setting own timeout value when constructing JedisPool using the following constructor: JedisPool(GenericObjectPool.Config poolConfig, String host, int port, int timeout) Default

在Java中进行api调用时,出现“Exception in thread "main" java.net.SocketTimeoutException: timeout”,“Caused by: java.net.SocketTimeoutException: Read timed out”的错误

在本地使用 OkHttpClient 创建请求客户端,调试api接口时,出现了表示中所述的错误,先贴出完整错误代码 Exception in thread "main" java.net.SocketTimeoutException: timeout at okio.Okio$4.newTimeoutException(Okio.java:230) at okio.AsyncTimeout.exit(AsyncTimeout.java:285) at okio.AsyncTimeout

java.net.SocketTimeoutException: Read timed out 异常排查

问题描述:使用RestTemplate调用接口出现该异常,相关调用代码: ResponseEntity<ResultVO> responseEntity; try { responseEntity = restTemplate.getForEntity( url, ResultVO.class ); } catch (Exception e) { log.error("ops获取项目失败" + e.getMessage()); throw new OAuth2Excepti

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect time out

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed outat redis.clients.jedis.Connection.connect(Connection.java:154)at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:83)at redis.clients

http://ibatis.apache.org/dtd/ibatis-3-config.dtd Cause: java.net.ConnectException: Connection timed out: connect

最近发现我的一个web项目只要在家启动时候就出现一个连接错误的问题,大概如下: Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderVersionMapper' defined in class path resource [applicationContext.xml]: Cannot resolve reference to be

eclipse使用STS插件 报错:SocketTimeoutException: Read timed out

新建boot项目后,提示: SocketTimeoutException: Read timed out 解决: 在eclipse.ini末尾,追加: -Djava.net.preferIPv4Stack=true 然后, 重启Eclipse,搞定. 原文地址:https://www.cnblogs.com/yaoyuan2/p/10348203.html

How to solve java.net.SocketTimeoutException:60000millis problem in HDFS

Many HDFS users encounter the following error when DFSClient ready file from a certain Data Node.  "Java.net.SocketTimeoutException: 60000millis timeout while waiting for channel to be ready for read. ch" The reason about this error is that the

连接mysql时提示java.sql.SQLException: Access denied for user &#39;root&#39;@&#39;DESKTOP-N2B2D9A&#39; (using password: YES)

用root连接mysql时提示:访问被拒绝 检查一下mysql server是否开启,发现后台在运行着..  然后查了一下mysql的用户表,发现root只能运行使用本地ip(localhost或者127.0.0.1)进行连接 解决方法:新开一个权限GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'jeffw' WITH GRANT OPTION; 然后查询一下:select * from user; 新开的root的已经开启权限,

Jsoup实现java模拟登陆

Jsoup实现java模拟登陆 1:如何获取cookies. 1.1:由于需要登录,故先模拟登陆时的那一个<form>,这里用map来装载变量名称,变量值. Map<String, String> map = new HashMap<String, String>(); map.put("username", username); map.put("pwd", md5.getMD5ofStr(password)); map.put