HttpClient超时的用法

HttpClient在运用中有两个超时时刻,是一向接触和运用的,由于前次工作中运用httpClient造成了体系悲惨剧的情况,特别对它的两个超时时刻进行了小小的测验,在这里记录下。

测验版别为HttpClient——3.1

一:衔接超时:connectionTimeout

1:指的是衔接一个url的衔接等待时刻。

2:设置办法为:

public class TestHttpClientMain {

/**

* @param args

*/

public static void main(String[] args) {

HttpClient client = new HttpClient();

HttpMethod method = new GetMethod(

"http://test.com");

client.getHttpConnectionManager().getParams()

.setConnectionTimeout(3000);

client.getHttpConnectionManager().getParams().setSoTimeout(3000);

try {

int statusCode = client.executeMethod(method);

System.out.println(statusCode);

byte[] responseBody = null;

responseBody = method.getResponseBody();

String result = new String(responseBody);

System.out.println(result);

} catch (HttpException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

3:测验的时分,将url改为一个不存在的url:“http://test.com”

4:超时时刻3000ms往后,体系报出异常。

org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 3000 ms

at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:155)

at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)

at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)

at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)

at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)

at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)

二:读取数据超时:soTimeout

1:指的是衔接上一个url,获取response的回来等待时刻

2:设置办法

public class TestHttpClientMain {

/**

* @param args

*/

public static void main(String[] args) {

HttpClient client = new HttpClient();

HttpMethod method = new GetMethod(

"http://localhost:8080/firstTest.htm?method=test");

client.getHttpConnectionManager().getParams()

.setConnectionTimeout(3000);

client.getHttpConnectionManager().getParams().setSoTimeout(2000);

try {

int statusCode = client.executeMethod(method);

System.out.println(statusCode);

byte[] responseBody = null;

responseBody = method.getResponseBody();

String result = new String(responseBody);

System.out.println(result);

} catch (HttpException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

3:测验的时分的衔接url为我本地敞开的一个url,http://localhost:8080/firstTest.htm?method=test

在我这个测验url里,当访问到这个连接时,线程sleep一段时刻,来模仿回来response超时。

@RequestMapping(params = "method=test") //<——②

public String testMethod(ModelMap model) {

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("call testMethod method.");

model.addAttribute("name", "test method");

return "test";

}

4:将读取response回来超时时刻设的时刻比那个sleep时刻短之后,运行程序给出异常:

java.net.SocketTimeoutException: Read timed out

at java.net.SocketInputStream.socket("http://www.fanselang.com","plus");

at java.net.SocketInputStream.socket("http://www.haomad.com","plus");

at java.net.SocketInputStream.socket("http://www.metabase.cn","plus");

at java.net.SocketInputStream.socket("http://www.3h5.cn","plus");

at java.net.SocketInputStream.socket("http://www.4lunwen.cn","plus");

at java.net.SocketInputStream.socket("http://www.zx1234.cn","plus");

at java.net.SocketInputStream.socket("http://www.majiangji168.cn","plus");

at java.net.SocketInputStream.socket("http://www.penbar.cn","plus");

at java.net.SocketInputStream.socket("http://www.whathappy.cn","plus");

at java.net.SocketInputStream.socket("http://www.lunjin.net","plus");

at java.net.SocketInputStream.socket("http://www.ssstyle.cn","plus");

at java.net.SocketInputStream.socket("http://www.91fish.cn","plus");

at java.net.SocketInputStream.socketRead0(Native Method)

at java.net.SocketInputStream.read(Unknown Source)

at java.io.BufferedInputStream.fill(Unknown Source)

at java.io.BufferedInputStream.read(Unknown Source)

at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)

at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)

at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)

at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)

at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)

ok,今后再写httpClient这两个超时时刻一定要加上,不加就很能够悲惨剧的了。

HttpClient超时的用法,布布扣,bubuko.com

时间: 2024-10-13 12:19:22

HttpClient超时的用法的相关文章

(HttpClient超时机制)timeout调度算法探讨

mark一下: HttpClient超时机制(安全问题处理:访问超大文件控制) 不过httpclient4开始好象没有MultiThreadedHttpConnectionManager这个类了,代替的应该是:PoolingHttpClientConnectionManager.

HttpClient超时设置

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

HttpClient的get用法

httpClient(执行时都需要放进线程中): try { String path="路径"; HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet(path); HttpResponse response = httpClient.execute(httpGet); int code = response.getStatusLine().getStatusCode(); if(code=

Android学习笔记:利用httpclient和AsyncTask 发起网络http post操作

1.在android4中,发起网络http操作,不能在Activity的事件(即主线程)中进行,必须在单独的线程中操作. 另外进行网络操作,需要在manifest文件中增加如下的权限: <uses-permission android:name="android.permission.INTERNET" /> 2.下面给出代码事例 public void loadData() { new AsyncTask<String, Void, String>() { @

HttpClient的一种简单实现Demo

1 /** 2 * 测试HttpClient2种请求网络方式的Activity 3 * get和post 4 * 5 */ 6 public class HttpClientActivity extends Activity { 7 private HttpParams httpParams ; 8 private HttpClient httpClient ; 9 @Override 10 protected void onCreate(Bundle savedInstanceState) {

Android 通过httpclient请求web服务器,并解决用户登录session保持

package com.rainet.tiis.network; import java.util.Iterator;import java.util.List;import java.util.Map; import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.CookieStore;import org.apache.http.client.Ht

[Java]使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图

  第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到”煎蛋网xxoo”网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保存至本地. 先放结果: 从程序来讲,步骤分为三步: 1.发起一个http请求,获取返回的response内容: 2.解析内容,分离出有效图片的url: 3.根据这些图片的url,生成图片保存至本地. 开始详细说明: 准备工作:HttpClient的Jar包,访问http://hc.apache.org/   自行下

php set_time_limit()用法测试详解

在php中set_time_limit函数是用来限制页面执行时间的,如我想把一个php页面的执行时间定义为5秒就可以set_time_limit(5)了. 一个php脚本通过crontab每5分钟执行一次,考虑到脚本执行时间会超过5分钟,特意用set_time_limit(290)来控制脚本在290秒退出.某天突然发现后台有多个该脚本的进程在执行,也就是说set_time_limit(290)没有起作用.为了证明,特意使用如下代码测试.  代码如下 复制代码 set_time_limit(5);

HttpClient在多线程环境下踩坑总结

问题现场 在多线程环境下使用HttpClient组件对某个HTTP服务发起请求,运行一段时间之后发现客户端主机CPU利用率呈现出下降趋势,而不是一个稳定的状态. 而且,从程序日志中判断有线程处于夯住的状态,应该是被阻塞了. 问题排查 一开始找不到原因,怀疑是多线程并发导致的死锁问题,但是通过代码审查并未定位到任何可能的多线程并发问题. 甚至开始怀疑是否是因为内存资源不够引起JVM频繁GC到导致业务线程被暂停,但是从GC的日志输出结果看,GC是正常的. 于是,进入一种丈二和尚摸不着头脑头脑的状态,