今天做了一个Jsoup解析网站的项目,使用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.jb51.net";
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
at
java.net.SocketInputStream.socketRead0(Native Method)
at
java.net.SocketInputStream.read(Unknown Source)
at
java.net.SocketInputStream.read(Unknown Source)
at
java.io.BufferedInputStream.fill(Unknown Source)
at
java.io.BufferedInputStream.read1(Unknown Source)
at
java.io.BufferedInputStream.read(Unknown Source)
at
sun.net.www.http.ChunkedInputStream.fastRead(Unknown Source)
at
sun.net.www.http.ChunkedInputStream.read(Unknown Source)
at
java.io.FilterInputStream.read(Unknown Source)
at
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown
Source)
at java.util.zip.InflaterInputStream.fill(Unknown Source)
at
java.util.zip.InflaterInputStream.read(Unknown Source)
at
java.util.zip.GZIPInputStream.read(Unknown Source)
at
java.io.BufferedInputStream.read1(Unknown Source)
at
java.io.BufferedInputStream.read(Unknown Source)
at
java.io.FilterInputStream.read(Unknown Source)
at
org.jsoup.helper.DataUtil.readToByteBuffer(DataUtil.java:113)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:447)
at
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:393)
at
org.jsoup.helper.HttpConnection.execute(HttpConnection.java:159)
at
org.jsoup.helper.HttpConnection.get(HttpConnection.java:148)
at
jsoupTest.JsoupTest.main(JsoupTest.java:17)
Time is:3885ms
Exception in
thread "main" java.lang.NullPointerException
at
jsoupTest.JsoupTest.main(JsoupTest.java:25)

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.jb51.net";
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());
}
}

Exception in thread "main" java.net.SocketTimeoutException: connect timed ou错误处理的更多相关文章

  1. Java 控制台执行带自定义包定义的类,出现“Exception in thread "main" java.lang.NoClassDefFoundError: ConnectSQLServer (wrong name: sine/ConnectSQLServer)”

    1.先说明一下代码实现:自定义package sine, 源代码保存路径为:E:\JSP\HibernateDemo\HibernateDemoProject\src\sine\ConnectSQLS ...

  2. xom报错 Exception in thread "main" java.net.UnknownHostException: file

    Exception in thread "main" java.net.UnknownHostException: file at java.net.AbstractPlainSo ...

  3. Exception in thread "main" java.nio.channels.NotYetConnectedException

    import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocke ...

  4. Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for topic_test_1219-2: 30010 ms has passed since batch creatio

    代码如下 public static void producer1() throws ExecutionException, InterruptedException { Properties pro ...

  5. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...

  6. Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V

    在学习CGlib动态代理时,遇到如下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.objectwe ...

  7. GUI学习中错误Exception in thread "main" java.lang.NullPointerException

    运行时出现错误:Exception in thread "main" java.lang.NullPointerException 该问题多半是由于用到的某个对象只进行了声明,而没 ...

  8. 执行打的maven jar包时出现“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”

    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for ...

  9. Exception in thread "main" java.lang.ExceptionInInitializerError

    Exception in thread "main" java.lang.ExceptionInInitializerErrorCaused by: java.util.Missi ...

随机推荐

  1. bzoj 4631: 踩气球 线段树合并

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 265  Solved: 136[Submit][Status][Discuss] ...

  2. ubuntu下sublime Text3配置C++编译环境

    今天在Ubuntu下用sublime Text3编译C++代码,环境配的不太顺利,下边展示一个实例. 1.主函数main.cpp #include <iostream> #include ...

  3. PHP多条件模糊查询

    所使用的方法:$sqlArr=array();array_push();implode(); 原理, 一.建立sql语句前半句,并且建立一个空数组. 二.根据条件是否为空来判断是否向数组中添加元素.如 ...

  4. centos7搭建ELK Cluster集群日志分析平台

    应用场景:ELK实际上是三个工具的集合,ElasticSearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构, 很多公司利用它来搭建可视化的海量日志分析平台 ...

  5. SQL记录-PLSQL事务

    PL/SQL事务   数据库事务是一个工作的原子单元,其可以由一个或多个相关的SQL语句组成.所谓的原子性就是数据库的修改所带来的构成事务的SQL语句可以集体被提交,即永久到数据库或从数据库中(撤消) ...

  6. bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器

    http://www.lydsy.com/JudgeOnline/problem.php?id=3122 等比数列求和公式+BSGS #include<map> #include<c ...

  7. 赫夫曼树JAVA实现及分析

    一,介绍 1)构造赫夫曼树的算法是一个贪心算法,贪心的地方在于:总是选取当前频率(权值)最低的两个结点来进行合并,构造新结点. 2)使用最小堆来选取频率最小的节点,有助于提高算法效率,因为要选频率最低 ...

  8. python AjaxSpider 代码演示

    import re # 引入正则表达式 import json # 引入 json import pymongo # 引入mongo数据库 import requests # 引入HTTP请求协议 f ...

  9. ASP.NET生成二维码

    下面使用ThoughtWorks.QRCode.dll这个类库,实现生成二维码 使用时需要增加:下面三个命名空间 using ThoughtWorks.QRCode.Codec; using Thou ...

  10. ubuntu 开机自动挂载分区

    转载: http://blog.sina.com.cn/s/blog_142e95b170102vx2a.html 我的计算机是双硬盘,一个是windows系统,一个是Fedora和ubuntu系统. ...