commons-httpclient中的超时设置

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) 主要代码逻辑:


  1  public int executeMethod(HostConfiguration hostconfig,
2 final HttpMethod method, final HttpState state)
3 throws IOException, HttpException {
4
5 LOG.trace("enter HttpClient.executeMethod(HostConfiguration,HttpMethod,HttpState)");
6
7 if (method == null) {
8 throw new IllegalArgumentException("HttpMethod parameter may not be null");
9 }
10 HostConfiguration defaulthostconfig = getHostConfiguration();
11 if (hostconfig == null) {
12 hostconfig = defaulthostconfig;
13 }
14 URI uri = method.getURI();
15 if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
16 // make a deep copy of the host defaults

17 hostconfig = (HostConfiguration) hostconfig.clone();
18 if (uri.isAbsoluteURI()) {
19 hostconfig.setHost(uri);
20 }
21 }
22
23 HttpMethodDirector methodDirector = new HttpMethodDirector(
24 getHttpConnectionManager(),
25 hostconfig,
26 this.params,
27 (state == null ? getState() : state));
28 methodDirector.executeMethod(method);
29 return method.getStatusCode();
30 }
31
32 //------------------------------
33
34 /**
35 * Executes the method associated with this method director.
36 *
37 * @throws IOException
38 * @throws HttpException
39 */
40 public void executeMethod(final HttpMethod method) throws IOException, HttpException {
41 //...
42 try {
43 int maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS, 100);
44
45 for (int redirectCount = 0;;) {
46
47 // make sure the connection we have is appropriate
48 if (this.conn != null && !hostConfiguration.hostEquals(this.conn)) {
49 this.conn.setLocked(false);
50 this.conn.releaseConnection();
51 this.conn = null;
52 }
53
54 // get a connection, if we need one
55 if (this.conn == null) {
56 this.conn = connectionManager.getConnectionWithTimeout(
57 hostConfiguration,
58 this.params.getConnectionManagerTimeout()
59 );
60 this.conn.setLocked(true);
61 if (this.params.isAuthenticationPreemptive()
62 || this.state.isAuthenticationPreemptive())
63 {
64 LOG.debug("Preemptively sending default basic credentials");
65 method.getHostAuthState().setPreemptive();
66 method.getHostAuthState().setAuthAttempted(true);
67 if (this.conn.isProxied() && !this.conn.isSecure()) {
68 method.getProxyAuthState().setPreemptive();
69 method.getProxyAuthState().setAuthAttempted(true);
70 }
71 }
72 }
73 } finally {
74 if (this.conn != null) {
75 this.conn.setLocked(false);
76 }
77 // If the response has been fully processed, return the connection
78 // to the pool. Use this flag, rather than other tests (like
79 // responseStream == null), as subclasses, might reset the stream,
80 // for example, reading the entire response into a file and then
81 // setting the file as the stream.
82 if (
83 (releaseConnection || method.getResponseBodyAsStream() == null)
84 && this.conn != null
85 ) {
86 this.conn.releaseConnection();
87 }
88 }
89
90 }
91
92 //-------------------------- this.conn = connectionManager.getConnectionWithTimeout
93
94 public HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration,
95 long timeout) throws ConnectionPoolTimeoutException {
96 //...
97 final HttpConnection conn = doGetConnection(hostConfiguration, timeout);
98
99 // wrap the connection in an adapter so we can ensure it is used
100 // only once
101 return new HttpConnectionAdapter(conn);
102 }

commons-httpclient中的超时设置

时间: 2024-08-07 00:06:35

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

nginx中的超时设置

参考博文: nginx中的超时设置 nginx使用proxy模块时,默认的读取超时时间是60s. 1. send_timeout syntax: send_timeout the time default: send_timeout 60 context: http, server, location Directive assigns response timeout to client. Timeout is established not on entire transfer of ans

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

可用的code public static String doPost(String url, String params, String contentType) /*throws IOException */{ CloseableHttpClient client = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(httpSoTimeout)

cURL中的超时设置

访问HTTP方式很多,可以使用curl, socket, file_get_contents() 等方法. 在访问http时,需要考虑超时的问题. CURL访问HTTP: CURL 是常用的访问HTTP协议接口的lib库,性能高,还有一些并发支持的功能等.  curl_setopt($ch, opt) 可以设置一些超时的设置,主要包括:    ① (重要) CURLOPT_TIMEOUT 设置cURL允许执行的最长秒数.      ② (重要) CURLOPT_TIMEOUT_MS 设置cURL

linux网络编程中的超时设置

1 下面是在网上找到的资料,先非常的感谢. 用setsockopt()来控制recv()与send()的超时 在send(),recv()过程中有时由于网络状况等原因,收发不能预期进行,而设置收发超时控制: 在Linux下需要注意的是时间的控制结构是struct timeval而并不是某一整型数,int nNetTimeout=1000;//1秒, //设置发送超时 setsockopt(socket,SOL_SOCKET,SO_SNDTIMEO,(char *)&nNetTimeout,siz

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

statement_timeout控制语句执行时长,单位是ms.超过设定值,该语句将被中止.不推荐在postgresql.conf中设置,因为会影响所有的会话,如非要设置,应该设置一个较大值. lock_timeout锁等待超时.语句在试图获取表.索引.行或其他数据库对象上的锁时等到超过指定的毫秒数,该语句将被中止.不推荐在postgresql.conf中设置,因为会影响所有的会话. idle_in_transaction_session_timeout终止开启事务但空闲时间超过指定持续时间(以

jQuery中ajax超时设置

var ajaxTimeoutTest = $.ajax({ url: '', timeout: 100, //超时时间设置,单位毫秒 type: 'get', data: {}, //请求所传参数,json格式 dataType: 'json', //返回的数据格式 success: function (data) { alert("成功"); }, complete: function (XMLHttpRequest, status) { //求完成后最终执行参数 // 设置tim

Java中httpClient中三种超时设置

本文章给大家介绍一下关于Java中httpClient中的三种超时设置小结 在Apache的HttpClient包中,有三个设置超时的地方: /* 从连接池中取连接的超时时间*/ ConnManagerParams.setTimeout(params, 1000); /*连接超时*/ HttpConnectionParams.setConnectionTimeout(params, 2000); /*请求超时*/ HttpConnectionParams.setSoTimeout(params,

HttpClient超时设置

场景:最近并发较高,看到响应时间6s的时候,心里咯噔一下,我记得我设置的超时时间是5s啊.   原来读取超时时间没生效,只生效了连接超时时间. ConnectionTimeout:这定义了通过网络与服务器建立连接的超时时间.Httpclient包中通过一个异步线程去创建与服务器的socket连接,这就是该socket连接的超时时间,此处设置为3秒. 将url改为一个不存在的url,则会抛出org.apache.commons.httpclient.ConnectTimeoutException

org.apache.commons.httpclient

org.apache.commons.httpclient 1 /** 2 * post 方法 3 * @param url 4 * @param params 5 * @return 6 */ 7 public static String post(String url, Object content, String encode) throws Exception { 8 9 byte[] responseBody = null; 10 HttpClient httpclient = new