java - Java 1.5 下 HTTP 连接超时

标签 java http timeout

我已经使用 java.net.HttpURLConnection 开发了对 Web 服务的简单低级访问。

我需要为连接和读取设置超时,这在 Java 1.6 及更高版本下似乎是可行的,但对于 Java 1.5 我完全不知所措。

是否有任何 HttpURLConnection 的替代品可以满足我的需求?

我现在的代码是这样的:

        oURL = new URL( this.endpoint );
        httpCon = (HttpURLConnection)oURL.openConnection();

        httpCon.setDoOutput(true);
        httpCon.setDoInput(true);

        httpCon.setConnectTimeout( this.connect_timeout );
        httpCon.setReadTimeout( this.read_timeout );

        httpCon.setRequestMethod("POST");
        httpCon.setRequestProperty("Content-type", "text/xml; charset=utf-8");
        httpCon.setRequestProperty("SOAPAction", this.endpoint+"#"+req.getOperationName());


        reqStream = httpCon.getOutputStream();
        reqStream.write( reqXML.getBytes());

一旦我从输出流中读取了响应,我就完成了。但是HttpURLConnection在1.5下的实现好像缺少这两个相关的方法。

最佳答案

我认为您没有使用 JDK 1.5,我在 eclipse 中创建了一个 java (1.5) 项目,它允许我使用这两种方法。

    HttpURLConnection a = (HttpURLConnection)new URL("URL").openConnection();
    a.setConnectTimeout(0);
    a.setReadTimeout(0);

在方法的描述中它说它是从 1.5 开始的

设置连接超时

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 implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().

Parameters: timeout an int that specifies the connect timeout value in milliseconds Throws: IllegalArgumentException - if the timeout parameter is negative Since: 1.5

设置读取超时

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().

Parameters: timeout an int that specifies the timeout value to be used in milliseconds Throws: IllegalArgumentException - if the timeout parameter is negative Since: 1.5

您还可以查看来自 oracle 的 API 文档

http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#setConnectTimeout(int)

http://docs.oracle.com/javase/1.5.0/docs/api/java/net/HttpURLConnection.html

它说 HttpURLConnection 继承自 URLConnection 并且这两个方法是 public 所以它们在 HttpURLConnection 类中.

关于java - Java 1.5 下 HTTP 连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34000133/

相关文章:

php - Apache 和/或 PHP 超时 - 难住了。

java - Java上创建的HttpSession可以使用PHP的$_SESSION来访问吗

java - 是否可以创建一个等待整个 try block 执行的 catch block ?

c - 如何在没有 libcurl 的情况下在 C 中发出 HTTP get 请求?

http - Ionic2 中的多个 $http 请求间隔

android - 如果 Android 设备不可用,Jenkins 构建会挂起

java - 在 Eclipse Kepler 中制作 toString() 方法来打印继承的字段

java - hibernate 无法重新连接到 mysql 数据库

node.js - NodeJS 相当于一个curl 请求

mysql - 如何防止大型MySQL导入的连接超时