connectionTimeout与soTimeout的差异,前者指创建一个有效的客户端到服务端链接的最大允许时间,后者指socket接收data的时间。

connectionManager.getParams().setConnectionTimeout(50);
connectionManager.getParams().setSoTimeout(100);

调用connectionTimeout属性的代码:

//org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory
public static Socket createSocket(final SocketTask task, int timeout)
throws IOException, UnknownHostException, ConnectTimeoutException{
try {
TimeoutController.execute(task, timeout);
} catch (TimeoutController.TimeoutException e) {
throw new ConnectTimeoutException(
"The host did not accept the connection within timeout of "
+ timeout + " ms");
}
Socket socket = task.getSocket();
if (task.exception != null) {
throw task.exception;
}
return socket;
}
//org.apache.commons.httpclient.util.TimeoutController
public static void execute(Thread task, long timeout) throws TimeoutException {
task.start();
try {
task.join(timeout);
} catch (InterruptedException e) {
/* if somebody interrupts us he knows what he is doing */
}
if (task.isAlive()) {
task.interrupt();
throw new TimeoutException();
}
}

调用soTimeout属性的代码:

socket.setSoTimeout(this.params.getSoTimeout());

* HttpClient.executeMethod(method) 主要代码逻辑:

  public int executeMethod(HostConfiguration hostconfig,
final HttpMethod method, final HttpState state)
throws IOException, HttpException { LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)"); if (method == null) {
throw new IllegalArgumentException("HttpMethod parameter may not be null");
}
HostConfiguration defaulthostconfig = getHostConfiguration();
if (hostconfig == null) {
hostconfig = defaulthostconfig;
}
URI uri = method.getURI();
if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
// make a deep copy of the host defaults
hostconfig = (HostConfiguration) hostconfig.clone();
if (uri.isAbsoluteURI()) {
hostconfig.setHost(uri);
}
} HttpMethodDirector methodDirector = new HttpMethodDirector(
getHttpConnectionManager(),
hostconfig,
this.params,
(state == null ? getState() : state));
methodDirector.executeMethod(method);
return method.getStatusCode();
} //------------------------------ /**
* Executes the method associated with this method director.
*
* @throws IOException
* @throws HttpException
*/
public void executeMethod(final HttpMethod method) throws IOException, HttpException {
//...
try {
int maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS, 100); for (int redirectCount = 0;;) { // make sure the connection we have is appropriate
if (this.conn != null && !hostConfiguration.hostEquals(this.conn)) {
this.conn.setLocked(false);
this.conn.releaseConnection();
this.conn = null;
} // get a connection, if we need one
if (this.conn == null) {
this.conn = connectionManager.getConnectionWithTimeout(
hostConfiguration,
this.params.getConnectionManagerTimeout()
);
this.conn.setLocked(true);
if (this.params.isAuthenticationPreemptive()
|| this.state.isAuthenticationPreemptive())
{
LOG.debug("Preemptively sending default basic credentials");
method.getHostAuthState().setPreemptive();
method.getHostAuthState().setAuthAttempted(true);
if (this.conn.isProxied() && !this.conn.isSecure()) {
method.getProxyAuthState().setPreemptive();
method.getProxyAuthState().setAuthAttempted(true);
}
}
}
} finally {
if (this.conn != null) {
this.conn.setLocked(false);
}
// If the response has been fully processed, return the connection
// to the pool. Use this flag, rather than other tests (like
// responseStream == null), as subclasses, might reset the stream,
// for example, reading the entire response into a file and then
// setting the file as the stream.
if (
(releaseConnection || method.getResponseBodyAsStream() == null)
&& this.conn != null
) {
this.conn.releaseConnection();
}
} } //-------------------------- this.conn = connectionManager.getConnectionWithTimeout public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration,
long timeout) throws ConnectionPoolTimeoutException {
//...
final HttpConnection conn = doGetConnection(hostConfiguration, timeout); // wrap the connection in an adapter so we can ensure it is used
// only once
return new HttpConnectionAdapter(conn);
}

commons-httpclient中的超时设置的更多相关文章

  1. PHP socket 编程中的超时设置

    PHP socket 编程中的超时设置.网上找了半天也没找到.贴出来分享之:设置$socket 发送超时1秒,接收超时3秒: $socket = socket_create(AF_INET,SOCK_ ...

  2. org.apache.http.client.HttpClient; HttpClient 4.3超时设置

    可用的code import org.apache.commons.lang.StringUtils;import org.apache.http.HttpEntity;import org.apac ...

  3. Linux串口中的超时设置

    在Linux下使用串口通信时,默认的阻塞模式是不实用的.而采用select或epoll机制的非阻塞模式,写代码有比较麻烦.幸好Linux的串口自己就带有超时机制. Linux下使用termios.h中 ...

  4. nginx中的超时设置,请求超时、响应等待超时等

    nginx比较强大,可以针对单个域名请求做出单个连接超时的配置. 比如些动态解释和静态解释可以根据业务的需求配置 proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响 ...

  5. nginx中的超时设置

    nginx使用proxy模块时,默认的读取超时时间是60s. 1. send_timeout syntax: send_timeout the time default: send_timeout 6 ...

  6. cURL中的超时设置

    访问HTTP方式很多,可以使用curl, socket, file_get_contents() 等方法. 在访问http时,需要考虑超时的问题. CURL访问HTTP: CURL 是常用的访问HTT ...

  7. linux网络编程中的超时设置

    1 下面是在网上找到的资料,先非常的感谢. 用setsockopt()来控制recv()与send()的超时 在send(),recv()过程中有时由于网络状况等原因,收发不能预期进行,而设置收发超时 ...

  8. pg中与超时设置有关的参数

    statement_timeout控制语句执行时长,单位是ms.超过设定值,该语句将被中止.不推荐在postgresql.conf中设置,因为会影响所有的会话,如非要设置,应该设置一个较大值. loc ...

  9. 原 HttpClient 4.3超时设置

    https://my.oschina.net/u/577453/blog/173724 http://blog.csdn.net/zh521zh/article/details/51994140

随机推荐

  1. jsp学习之基于mvc学生管理系统的编写

    mvc开发模式:分别是 model层 view层 Control层 在学生管理系统中,model层有学生实体类,数据访问的dao层,view层主要是用于显示信息的界面,Control层主要是servl ...

  2. golang实现随机数

    package main import ( "fmt" "time" "math/rand" ) func main() { r := ra ...

  3. iOS 为移动中的UIView(UIButton )添加点击事件

    高高兴兴迎接新的产品新需求,满心欢喜的开始工作,结果研究了一下午才发现,是自己想的太简单了,是我太单纯呀. 需求是这样的类似下雪的效果,随机产生一些小雪花,然后每个雪花可以点击到下个页面. 接到需求之 ...

  4. 2.4G/5G频段WLAN的模式、带宽、协商速率

    2.4G频段 5G频段

  5. c++继承,多态,重载的作用

    继承:用户通过继承,可以从一个类派生出多个子类,减少了重新定义类的次数,并且在不影响子类的相应功能的情况下,保护了基类中的数据安全性.用户还可以在子类中,使用与基类相同的行为实现不同的功能,因此,使用 ...

  6. Spring中的IOC\DI\AOP等概念的简单学习

    IoC(Inversion of Control,控制反转).这是spring的核心,贯穿始终, 所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系.Spr ...

  7. Myeclipse+AJAX+Servlet

    最近刚开始学AJAX的知识,这里介绍一个简单的Myeclipse+AJAX+Servlet项目. 此项目包含3个文件:index.jsp.check.java.还有一个需要配置的文件是:web.xml ...

  8. demo和实际项目的距离

    回家的路上想到一个很形象的类比,关于学生时期的实验(以及一些简单的demo)和实际工作项目的差别. 实现了同样的功能,比如要制作一把椅子,如果是简单的demo,那么就如同是给你了一个单独的房间,里面已 ...

  9. nodejs入门 SSH服务器远程部署nodejs2

    服务器安装nodejs昨天好像出了点问题 今天参考的链接是http://nodejs.cn/download/package-manager/#debian-and-ubuntu-based-linu ...

  10. Linux内核分析——分析system_call中断处理过程

    万子惠 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我选择的是get ...