HttpClient
httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取
HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。
connectManager分为:
SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。
MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,key=hostConfig value = connectPool,每次根据host从池子中获取连接,并复用之
DummyConnectionManager 则不管host,一律重新建立连接
每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:
- public class PoolableHttpClientFactory implements PoolableObjectFactory {
-
- private int timeout;
- public PoolableHttpClientFactory(int timeout) {
- this.timeout = timeout;
- }
-
- public Object makeObject() throws Exception {
- HttpClient httpClient = new HttpClient();
- HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
- configParams.setConnectionTimeout(timeout);
- configParams.setSoTimeout(timeout);
- httpClient.getParams().setConnectionManagerTimeout(timeout);
- return httpClient;
- }
-
- public void destroyObject(Object obj) throws Exception {
- }
-
- public boolean validateObject(Object obj) {
- return true;
- }
-
- public void activateObject(Object obj) throws Exception {
- }
-
- public void passivateObject(Object obj) throws Exception {
- }
-
- }
- ExecutorService s = Executors.newFixedThreadPool(2);
-
- final ObjectPool pool = new GenericObjectPool(
- new PoolableHttpClientFactory(2000),
- 10,
- GenericObjectPool.WHEN_EXHAUSTED_FAIL,
- 3000, 2, 1, false, false, 60000, 10, 60000, false);
-
- for(int i = 0; i < 2; ++i)
- s.execute(new Runnable() {
- public void run() {
- while(true) {
- try {
- Thread.sleep(new Random().nextInt(500));
-
- PostMethod method = new PostMethod("http://127.0.0.1:8080/index");
- HttpClient hc = (HttpClient) pool.borrowObject();
- hc.executeMethod(method);
- String resp = method.getResponseBodyAsString();
- if(resp.indexOf("jack") == -1) System.out.println(resp);
-
- method.releaseConnection();
- pool.returnObject(hc);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- });
TIP ===================
HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性
对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()
HttpURLConnection
1、new URL("http://xx.xx").openConnection(); 会打开 sun.net.www.protocol.http.HttpURLConnection, 其conn.connect()函数会从sun.net.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接,对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient
2、HttpURLConnection.getInputStream()会返回sun.net.www.protocol.http.HttpURLConnection$HttpInputStream,其connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。
HttpClient
httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取
HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。
connectManager分为:
SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。
MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,key=hostConfig value = connectPool,每次根据host从池子中获取连接,并复用之
DummyConnectionManager 则不管host,一律重新建立连接
每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:
- public class PoolableHttpClientFactory implements PoolableObjectFactory {
-
- private int timeout;
- public PoolableHttpClientFactory(int timeout) {
- this.timeout = timeout;
- }
-
- public Object makeObject() throws Exception {
- HttpClient httpClient = new HttpClient();
- HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
- configParams.setConnectionTimeout(timeout);
- configParams.setSoTimeout(timeout);
- httpClient.getParams().setConnectionManagerTimeout(timeout);
- return httpClient;
- }
-
- public void destroyObject(Object obj) throws Exception {
- }
-
- public boolean validateObject(Object obj) {
- return true;
- }
-
- public void activateObject(Object obj) throws Exception {
- }
-
- public void passivateObject(Object obj) throws Exception {
- }
-
- }
- ExecutorService s = Executors.newFixedThreadPool(2);
-
- final ObjectPool pool = new GenericObjectPool(
- new PoolableHttpClientFactory(2000),
- 10,
- GenericObjectPool.WHEN_EXHAUSTED_FAIL,
- 3000, 2, 1, false, false, 60000, 10, 60000, false);
-
- for(int i = 0; i < 2; ++i)
- s.execute(new Runnable() {
- public void run() {
- while(true) {
- try {
- Thread.sleep(new Random().nextInt(500));
-
- PostMethod method = new PostMethod("http://127.0.0.1:8080/index");
- HttpClient hc = (HttpClient) pool.borrowObject();
- hc.executeMethod(method);
- String resp = method.getResponseBodyAsString();
- if(resp.indexOf("jack") == -1) System.out.println(resp);
-
- method.releaseConnection();
- pool.returnObject(hc);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- });
TIP ===================
HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性
对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()
HttpURLConnection
1、new URL("http://xx.xx").openConnection(); 会打开 sun.net.www.protocol.http.HttpURLConnection, 其conn.connect()函数会从sun.net.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接,对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient
2、HttpURLConnection.getInputStream()会返回sun.net.www.protocol.http.HttpURLConnection$HttpInputStream,其connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。
HttpClient
httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取
HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。
connectManager分为:
SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。
MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,key=hostConfig value = connectPool,每次根据host从池子中获取连接,并复用之
DummyConnectionManager 则不管host,一律重新建立连接
每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:
- public class PoolableHttpClientFactory implements PoolableObjectFactory {
-
- private int timeout;
- public PoolableHttpClientFactory(int timeout) {
- this.timeout = timeout;
- }
-
- public Object makeObject() throws Exception {
- HttpClient httpClient = new HttpClient();
- HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
- configParams.setConnectionTimeout(timeout);
- configParams.setSoTimeout(timeout);
- httpClient.getParams().setConnectionManagerTimeout(timeout);
- return httpClient;
- }
-
- public void destroyObject(Object obj) throws Exception {
- }
-
- public boolean validateObject(Object obj) {
- return true;
- }
-
- public void activateObject(Object obj) throws Exception {
- }
-
- public void passivateObject(Object obj) throws Exception {
- }
-
- }
- ExecutorService s = Executors.newFixedThreadPool(2);
-
- final ObjectPool pool = new GenericObjectPool(
- new PoolableHttpClientFactory(2000),
- 10,
- GenericObjectPool.WHEN_EXHAUSTED_FAIL,
- 3000, 2, 1, false, false, 60000, 10, 60000, false);
-
- for(int i = 0; i < 2; ++i)
- s.execute(new Runnable() {
- public void run() {
- while(true) {
- try {
- Thread.sleep(new Random().nextInt(500));
-
- PostMethod method = new PostMethod("http://127.0.0.1:8080/index");
- HttpClient hc = (HttpClient) pool.borrowObject();
- hc.executeMethod(method);
- String resp = method.getResponseBodyAsString();
- if(resp.indexOf("jack") == -1) System.out.println(resp);
-
- method.releaseConnection();
- pool.returnObject(hc);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- });
TIP ===================
HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性
对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()
HttpURLConnection
1、new URL("http://xx.xx").openConnection(); 会打开 sun.net.www.protocol.http.HttpURLConnection, 其conn.connect()函数会从sun.net.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接,对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient
2、HttpURLConnection.getInputStream()会返回sun.net.www.protocol.http.HttpURLConnection$HttpInputStream,其connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。
HttpClient
httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取
HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。
connectManager分为:
SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。
MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,key=hostConfig value = connectPool,每次根据host从池子中获取连接,并复用之
DummyConnectionManager 则不管host,一律重新建立连接
每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:
- public class PoolableHttpClientFactory implements PoolableObjectFactory {
-
- private int timeout;
- public PoolableHttpClientFactory(int timeout) {
- this.timeout = timeout;
- }
-
- public Object makeObject() throws Exception {
- HttpClient httpClient = new HttpClient();
- HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
- configParams.setConnectionTimeout(timeout);
- configParams.setSoTimeout(timeout);
- httpClient.getParams().setConnectionManagerTimeout(timeout);
- return httpClient;
- }
-
- public void destroyObject(Object obj) throws Exception {
- }
-
- public boolean validateObject(Object obj) {
- return true;
- }
-
- public void activateObject(Object obj) throws Exception {
- }
-
- public void passivateObject(Object obj) throws Exception {
- }
-
- }
- ExecutorService s = Executors.newFixedThreadPool(2);
-
- final ObjectPool pool = new GenericObjectPool(
- new PoolableHttpClientFactory(2000),
- 10,
- GenericObjectPool.WHEN_EXHAUSTED_FAIL,
- 3000, 2, 1, false, false, 60000, 10, 60000, false);
-
- for(int i = 0; i < 2; ++i)
- s.execute(new Runnable() {
- public void run() {
- while(true) {
- try {
- Thread.sleep(new Random().nextInt(500));
-
- PostMethod method = new PostMethod("http://127.0.0.1:8080/index");
- HttpClient hc = (HttpClient) pool.borrowObject();
- hc.executeMethod(method);
- String resp = method.getResponseBodyAsString();
- if(resp.indexOf("jack") == -1) System.out.println(resp);
-
- method.releaseConnection();
- pool.returnObject(hc);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- });
TIP ===================
HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性
对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()
HttpURLConnection
1、new URL("http://xx.xx").openConnection(); 会打开 sun.net.www.protocol.http.HttpURLConnection, 其conn.connect()函数会从sun.net.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接,对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient
2、HttpURLConnection.getInputStream()会返回sun.net.www.protocol.http.HttpURLConnection$HttpInputStream,其connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。
HttpClient
httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取
HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。
connectManager分为:
SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。
MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,key=hostConfig value = connectPool,每次根据host从池子中获取连接,并复用之
DummyConnectionManager 则不管host,一律重新建立连接
每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:
- public class PoolableHttpClientFactory implements PoolableObjectFactory {
-
- private int timeout;
- public PoolableHttpClientFactory(int timeout) {
- this.timeout = timeout;
- }
-
- public Object makeObject() throws Exception {
- HttpClient httpClient = new HttpClient();
- HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
- configParams.setConnectionTimeout(timeout);
- configParams.setSoTimeout(timeout);
- httpClient.getParams().setConnectionManagerTimeout(timeout);
- return httpClient;
- }
-
- public void destroyObject(Object obj) throws Exception {
- }
-
- public boolean validateObject(Object obj) {
- return true;
- }
-
- public void activateObject(Object obj) throws Exception {
- }
-
- public void passivateObject(Object obj) throws Exception {
- }
-
- }
- ExecutorService s = Executors.newFixedThreadPool(2);
-
- final ObjectPool pool = new GenericObjectPool(
- new PoolableHttpClientFactory(2000),
- 10,
- GenericObjectPool.WHEN_EXHAUSTED_FAIL,
- 3000, 2, 1, false, false, 60000, 10, 60000, false);
-
- for(int i = 0; i < 2; ++i)
- s.execute(new Runnable() {
- public void run() {
- while(true) {
- try {
- Thread.sleep(new Random().nextInt(500));
-
- PostMethod method = new PostMethod("http://127.0.0.1:8080/index");
- HttpClient hc = (HttpClient) pool.borrowObject();
- hc.executeMethod(method);
- String resp = method.getResponseBodyAsString();
- if(resp.indexOf("jack") == -1) System.out.println(resp);
-
- method.releaseConnection();
- pool.returnObject(hc);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- });
TIP ===================
HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性
对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()
HttpURLConnection
1、new URL("http://xx.xx").openConnection(); 会打开 sun.net.www.protocol.http.HttpURLConnection, 其conn.connect()函数会从sun.net.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接,对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient
2、HttpURLConnection.getInputStream()会返回sun.net.www.protocol.http.HttpURLConnection$HttpInputStream,其connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。
HttpClient
httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取
HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。
connectManager分为:
SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。
MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,key=hostConfig value = connectPool,每次根据host从池子中获取连接,并复用之
DummyConnectionManager 则不管host,一律重新建立连接
每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:
- public class PoolableHttpClientFactory implements PoolableObjectFactory {
-
- private int timeout;
- public PoolableHttpClientFactory(int timeout) {
- this.timeout = timeout;
- }
-
- public Object makeObject() throws Exception {
- HttpClient httpClient = new HttpClient();
- HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
- configParams.setConnectionTimeout(timeout);
- configParams.setSoTimeout(timeout);
- httpClient.getParams().setConnectionManagerTimeout(timeout);
- return httpClient;
- }
-
- public void destroyObject(Object obj) throws Exception {
- }
-
- public boolean validateObject(Object obj) {
- return true;
- }
-
- public void activateObject(Object obj) throws Exception {
- }
-
- public void passivateObject(Object obj) throws Exception {
- }
-
- }
- ExecutorService s = Executors.newFixedThreadPool(2);
-
- final ObjectPool pool = new GenericObjectPool(
- new PoolableHttpClientFactory(2000),
- 10,
- GenericObjectPool.WHEN_EXHAUSTED_FAIL,
- 3000, 2, 1, false, false, 60000, 10, 60000, false);
-
- for(int i = 0; i < 2; ++i)
- s.execute(new Runnable() {
- public void run() {
- while(true) {
- try {
- Thread.sleep(new Random().nextInt(500));
-
- PostMethod method = new PostMethod("http://127.0.0.1:8080/index");
- HttpClient hc = (HttpClient) pool.borrowObject();
- hc.executeMethod(method);
- String resp = method.getResponseBodyAsString();
- if(resp.indexOf("jack") == -1) System.out.println(resp);
-
- method.releaseConnection();
- pool.returnObject(hc);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- });
TIP ===================
HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性
对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()
HttpURLConnection
1、new URL("http://xx.xx").openConnection(); 会打开 sun.net.www.protocol.http.HttpURLConnection, 其conn.connect()函数会从sun.net.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接,对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient
2、HttpURLConnection.getInputStream()会返回sun.net.www.protocol.http.HttpURLConnection$HttpInputStream,其connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。