setConnectionTimeout 、 setSoTimeout 和“http.connection-manager.timeout"之间有什么区别?在 Apache HttpClient API 中 [英] What is the difference between the setConnectionTimeout , setSoTimeout and "http.connection-manager.timeout" in apache HttpClient API

查看:41
本文介绍了setConnectionTimeout 、 setSoTimeout 和“http.connection-manager.timeout"之间有什么区别?在 Apache HttpClient API 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

三者有什么区别(标记为注释):

What is the difference between the three(marked as comments) :

MultiThreadedHttpConnectionManager connManag =  new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams managParams = connManag.getParams();

managParams.setConnectionTimeout(connectiontimeout); // 1
managParams.setSoTimeout(sotimeout); //2

HttpMethodBase baseMethod = null;

try {
  HttpClient client = new HttpClient(connManag);
  client.getParams().setParameter("http.connection-manager.timeout", poolTimeout); //3

  baseMethod = new GetMethod(…);
  int statusCode = client.executeMethod(…);

  …
}
catch (ConnectTimeoutException cte ){
  //Took too long to connect to remote host
}
catch (SocketTimeoutException ste){
  //Remote host didn’t respond in time
}
catch (Exception se){
  //Some other error occurred
}
finally {
  if (baseMethod != null)
    baseMethod.releaseConnection();
}

<代码>1.setConnectionTimeout - 如果它确定连接建立之前的超时时间.

1. setConnectionTimeout - if it determines the timeout until connection is established.

<代码>2.setSoTimeout - 如果它确定了两个连续数据包之间的不活动时间或时间差,

2. setSoTimeout - if it determines the period of inactivity or time difference between two consecutive packets ,

那么下面的一个做什么:

Then what does the below one do :

<代码>3."http.connection-manager.timeout"

推荐答案

HTTP 的最底层是 TCP 套接字.因此,当您请求一个 URL 并获得响应时,在较低级别,会创建一个客户端 Socket,它与远程服务器 Socket 建立连接,发送一些数据并接收响应.

At the lowest level HTTP is TCP socket. So when you request a URL and get a response, at lower level, a client Socket is created which establishes connection to the remote Server Socket, sends some data and receives response.

  • setConnectionTimeout :客户端尝试连接到服务器.这表示建立连接或服务器响应连接请求之前经过的时间.

  • setConnectionTimeout : Client tries to connect to the server. This denotes the time elapsed before the connection established or Server responded to connection request.

setSoTimeout :建立连接后,客户端套接字在发送请求后等待响应.这是自客户端在服务器响应之前向服务器发送请求以来经过的时间.请注意,这与服务器发送给客户端的 HTTP 错误 408 不同.换句话说,它在建立连接后到达客户端的两个连续数据包之间的最大不活动时间.

setSoTimeout : After establishing the connection, the client socket waits for response after sending the request. This is the elapsed time since the client has sent request to the server before server responds. Please note that this is not same as HTTP Error 408 which the server sends to the client. In other words its maximum period inactivity between two consecutive data packets arriving at client side after connection is established.

http.connection-manager.timeout:MultiThreadedHttpConnectionManager 使用HTTP 连接池.它为每个主机设置了最大值和最小值.如果特定主机的所有连接都达到最大值,则同一主机的新连接请求将不得不等待,直到现有连接中的任何一个空闲.此参数表示从发出连接请求到 HttpConnectionManager 返回连接之前经过的时间.

http.connection-manager.timeout : MultiThreadedHttpConnectionManager uses a pool of HTTP connections. It has maximum and minimum values per host set for it. If all the connections for particular host are has reached max value, the request for new connection for the same host will have to wait till any one of the existing connection becomes free. This parameter denotes the time elapsed when a connection request was made and before the HttpConnectionManager returned a connection.

这篇关于setConnectionTimeout 、 setSoTimeout 和“http.connection-manager.timeout"之间有什么区别?在 Apache HttpClient API 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆