connectTimeOut和readTimeout

Posted THISISPAN

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了connectTimeOut和readTimeout相关的知识,希望对你有一定的参考价值。

网络编程时,经常遇到很多timeout异常,下面是java URLConnection 中经典的2种 timeout参数,这些参数设置不当的话,就会遇到timeout 异常。

1. ConnectTimeout , java 是这样解释的。 意思是用来建立连接的时间。如果到了指定的时间,还没建立连接,则报异常。

 

 

Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout. 

Some non-standard implmentation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().

 

经典异常如下所示:

 

java.net.SocketTimeoutException: connect timed out
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1491)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1485)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
    at 。。。。。。(Unknown Source)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:564)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:158)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2300)
    at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:579)
    at java.net.URLConnection.getContentLength(URLConnection.java:474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContentLength(HttpsURLConnectionImpl.java:378)
    at 。。。。。。(Unknown Source)

 

通过 URLConnection setConnectTimeout 可以设置指定的值。0则指极大值。

        conn = url.openConnection();
                    
        conn.setConnectTimeout(5000);

 

2. ReadTimeout , java 是这样解释的。 意思是已经建立连接,并开始读取服务端资源。如果到了指定的时间,没有可能的数据被客户端读取,则报异常。

 

 

Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout. 

Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().


 

类似报错信息如下:

 

 

java.net.SocketTimeoutException: Read timed out
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1491)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1485)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
	at 。。。。。。(Unknown Source)
Caused by: java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.read(SocketInputStream.java:129)
	at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
	at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:830)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1170)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1197)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1181)
	at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
	at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2300)
	at java.net.URLConnection.getHeaderFieldInt(URLConnection.java:579)
	at java.net.URLConnection.getContentLength(URLConnection.java:474)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContentLength(HttpsURLConnectionImpl.java:378)
	at 。。。。。。(Unknown Source)

 

通过 URLConnection setReadTimeout 可以设置指定的值。0则指极大值。

        conn = url.openConnection();

        conn.setReadTimeout(20000);

 

类似的设置在很多软件都基本相同的,比如 wget.

 

 -T,  --timeout=SECONDS         set all timeout values to SECONDS.
      --dns-timeout=SECS        set the DNS lookup timeout to SECS.
      --connect-timeout=SECS    set the connect timeout to SECS.
      --read-timeout=SECS       set the read timeout to SECS.


 

 

 
 

,但是实际的情况是当时专网的时候(不能访问百度)根本不是1.5秒抛出超时的异常,这个问题困扰2天,

也在网上查询很多资料,结果有人说还要设置读取超时conn.setReadTimeout(1500);结果一试还是不行,

就再找发现有人在使用的情况下查看控制台发现访问1500毫秒之后系统会自动的尝试很多次(集体多少次没有算)

导致很长时间才抛出访问超时异常,这样导致我们看到的现象就是我设置了1500毫秒超时结果几分钟才给我超时异常,感觉设置的超时时间没有效果。

 

那么说一下我的解决方法,我个人测试通过,如果有好的方法请指出:

因为系统会自动的尝试很多次超时之后才给你抛出异常,所以我把这个异常超时时间设置短一点如我设置500毫秒,公网的情况下能正常的访问,专网也能很快的抛出异常达到了我的目的。

另外判断公网专网的另一种方法就是用ping ,记得要起一个服务区处理ping,关闭之后要杀死改进程,否则第二次ping可能就阻塞了。

 

以上是关于connectTimeOut和readTimeout的主要内容,如果未能解决你的问题,请参考以下文章

PHP cURL 超时设置 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT 的区别

我可以使用 Ribbon 配置 Feign 客户端 ConnectTimeout 和 ReadTimeout 覆盖吗?

httpclient connecttimeout sockettimeout区别

PHP cURL:CURLOPT_CONNECTTIMEOUT 与 CURLOPT_TIMEOUT

MongoClient 不尊重 connectTimeout

一定要为HttpUrlConnection设置connectTimeout属性以防止连接被阻塞