HttpURLConnection, HttpClient机制分析.docx_第1页
HttpURLConnection, HttpClient机制分析.docx_第2页
HttpURLConnection, HttpClient机制分析.docx_第3页
HttpURLConnection, HttpClient机制分析.docx_第4页
HttpURLConnection, HttpClient机制分析.docx_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

HttpURLConnection, HttpClient机制分析 HttpClienthttpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。connectManager分为:SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,keyhostConfig value = connectPool,每次根据host从池子中获取连接,并复用之DummyConnectionManager 则不管host,一律重新建立连接每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:javaview plaincopy 1. publicclassPoolableHttpClientFactoryimplementsPoolableObjectFactory2. 3. privateinttimeout;4. publicPoolableHttpClientFactory(inttimeout)5. this.timeout=timeout;6. 7. 8. publicObjectmakeObject()throwsException9. HttpClienthttpClient=newHttpClient();10. HttpConnectionManagerParamsconfigParams=httpClient.getHttpConnectionManager().getParams();11. configParams.setConnectionTimeout(timeout);12. configParams.setSoTimeout(timeout);13. httpClient.getParams().setConnectionManagerTimeout(timeout);14. returnhttpClient;15. 16. 17. publicvoiddestroyObject(Objectobj)throwsException18. 19. 20. publicbooleanvalidateObject(Objectobj)21. returntrue;22. 23. 24. publicvoidactivateObject(Objectobj)throwsException25. 26. 27. publicvoidpassivateObject(Objectobj)throwsException28. 29. 30. javaview plaincopy 1. ExecutorServices=Executors.newFixedThreadPool(2);2. 3. finalObjectPoolpool=newGenericObjectPool(4. newPoolableHttpClientFactory(2000),5. 10,6. GenericObjectPool.WHEN_EXHAUSTED_FAIL,7. 3000,2,1,false,false,60000,10,60000,false);8. 9. for(inti=0;i2;+i)10. s.execute(newRunnable()11. publicvoidrun()12. while(true)13. try14. Thread.sleep(newRandom().nextInt(500);15. 16. PostMethodmethod=newPostMethod(:8080/index);17. HttpClienthc=(HttpClient)pool.borrowObject();18. hc.executeMethod(method);19. Stringresp=method.getResponseBodyAsString();20. if(resp.indexOf(jack)=-1)System.out.println(resp);21. 22. method.releaseConnection();23. pool.returnObject(hc);24. catch(Exceptione)25. e.printStackTrace();26. 27. 28. 29. );TIP =HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()HttpURLConnection1、new URL(http:/xx.xx).openConnection(); 会打开 tocol.http.HttpURLConnection, 其conn.connect()函数会从.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接, 对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient2、HttpURLConnection.getInputStream()会返回 tocol.http.HttpURLConnection$HttpInputStream,其 connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。HttpClienthttpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。connectManager分为:SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,keyhostConfig value = connectPool,每次根据host从池子中获取连接,并复用之DummyConnectionManager 则不管host,一律重新建立连接每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:javaview plaincopy 1. publicclassPoolableHttpClientFactoryimplementsPoolableObjectFactory2. 3. privateinttimeout;4. publicPoolableHttpClientFactory(inttimeout)5. this.timeout=timeout;6. 7. 8. publicObjectmakeObject()throwsException9. HttpClienthttpClient=newHttpClient();10. HttpConnectionManagerParamsconfigParams=httpClient.getHttpConnectionManager().getParams();11. configParams.setConnectionTimeout(timeout);12. configParams.setSoTimeout(timeout);13. httpClient.getParams().setConnectionManagerTimeout(timeout);14. returnhttpClient;15. 16. 17. publicvoiddestroyObject(Objectobj)throwsException18. 19. 20. publicbooleanvalidateObject(Objectobj)21. returntrue;22. 23. 24. publicvoidactivateObject(Objectobj)throwsException25. 26. 27. publicvoidpassivateObject(Objectobj)throwsException28. 29. 30. javaview plaincopy 1. ExecutorServices=Executors.newFixedThreadPool(2);2. 3. finalObjectPoolpool=newGenericObjectPool(4. newPoolableHttpClientFactory(2000),5. 10,6. GenericObjectPool.WHEN_EXHAUSTED_FAIL,7. 3000,2,1,false,false,60000,10,60000,false);8. 9. for(inti=0;i2;+i)10. s.execute(newRunnable()11. publicvoidrun()12. while(true)13. try14. Thread.sleep(newRandom().nextInt(500);15. 16. PostMethodmethod=newPostMethod(:8080/index);17. HttpClienthc=(HttpClient)pool.borrowObject();18. hc.executeMethod(method);19. Stringresp=method.getResponseBodyAsString();20. if(resp.indexOf(jack)=-1)System.out.println(resp);21. 22. method.releaseConnection();23. pool.returnObject(hc);24. catch(Exceptione)25. e.printStackTrace();26. 27. 28. 29. );TIP =HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()HttpURLConnection1、new URL(http:/xx.xx).openConnection(); 会打开 tocol.http.HttpURLConnection, 其conn.connect()函数会从.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接, 对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient2、HttpURLConnection.getInputStream()会返回 tocol.http.HttpURLConnection$HttpInputStream,其 connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。HttpClient httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。connectManager分为:SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,keyhostConfig value = connectPool,每次根据host从池子中获取连接,并复用之DummyConnectionManager 则不管host,一律重新建立连接每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:javaview plaincopy 1. publicclassPoolableHttpClientFactoryimplementsPoolableObjectFactory2. 3. privateinttimeout;4. publicPoolableHttpClientFactory(inttimeout)5. this.timeout=timeout;6. 7. 8. publicObjectmakeObject()throwsException9. HttpClienthttpClient=newHttpClient();10. HttpConnectionManagerParamsconfigParams=httpClient.getHttpConnectionManager().getParams();11. configParams.setConnectionTimeout(timeout);12. configParams.setSoTimeout(timeout);13. httpClient.getParams().setConnectionManagerTimeout(timeout);14. returnhttpClient;15. 16. 17. publicvoiddestroyObject(Objectobj)throwsException18. 19. 20. publicbooleanvalidateObject(Objectobj)21. returntrue;22. 23. 24. publicvoidactivateObject(Objectobj)throwsException25. 26. 27. publicvoidpassivateObject(Objectobj)throwsException28. 29. 30. javaview plaincopy 1. ExecutorServices=Executors.newFixedThreadPool(2);2. 3. finalObjectPoolpool=newGenericObjectPool(4. newPoolableHttpClientFactory(2000),5. 10,6. GenericObjectPool.WHEN_EXHAUSTED_FAIL,7. 3000,2,1,false,false,60000,10,60000,false);8. 9. for(inti=0;i2;+i)10. s.execute(newRunnable()11. publicvoidrun()12. while(true)13. try14. Thread.sleep(newRandom().nextInt(500);15. 16. PostMethodmethod=newPostMethod(:8080/index);17. HttpClienthc=(HttpClient)pool.borrowObject();18. hc.executeMethod(method);19. Stringresp=method.getResponseBodyAsString();20. if(resp.indexOf(jack)=-1)System.out.println(resp);21. 22. method.releaseConnection();23. pool.returnObject(hc);24. catch(Exceptione)25. e.printStackTrace();26. 27. 28. 29. );TIP =HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()HttpURLConnection1、new URL(http:/xx.xx).openConnection(); 会打开 tocol.http.HttpURLConnection, 其conn.connect()函数会从.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接, 对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient2、HttpURLConnection.getInputStream()会返回 tocol.http.HttpURLConnection$HttpInputStream,其 connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。HttpClienthttpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。connectManager分为:SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,keyhostConfig value = connectPool,每次根据host从池子中获取连接,并复用之DummyConnectionManager 则不管host,一律重新建立连接每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:javaview plaincopy 1. publicclassPoolableHttpClientFactoryimplementsPoolableObjectFactory2. 3. privateinttimeout;4. publicPoolableHttpClientFactory(inttimeout)5. this.timeout=timeout;6. 7. 8. publicObjectmakeObject()throwsException9. HttpClienthttpClient=newHttpClient();10. HttpConnectionManagerParamsconfigParams=httpClient.getHttpConnectionManager().getParams();11. configParams.setConnectionTimeout(timeout);12. configParams.setSoTimeout(timeout);13. httpClient.getParams().setConnectionManagerTimeout(timeout);14. returnhttpClient;15. 16. 17. publicvoiddestroyObject(Objectobj)throwsException18. 19. 20. publicbooleanvalidateObject(Objectobj)21. returntrue;22. 23. 24. publicvoidactivateObject(Objectobj)throwsException25. 26. 27. publicvoidpassivateObject(Objectobj)throwsException28. 29. 30. javaview plaincopy 1. ExecutorServices=Executors.newFixedThreadPool(2);2. 3. finalObjectPoolpool=newGenericObjectPool(4. newPoolableHttpClientFactory(2000),5. 10,6. GenericObjectPool.WHEN_EXHAUSTED_FAIL,7. 3000,2,1,false,false,60000,10,60000,false);8. 9. for(inti=0;i2;+i)10. s.execute(newRunnable()11. publicvoidrun()12. while(true)13. try14. Thread.sleep(newRandom().nextInt(500);15. 16. PostMethodmethod=newPostMethod(:8080/index);17. HttpClienthc=(HttpClient)pool.borrowObject();18. hc.executeMethod(method);19. Stringresp=method.getResponseBodyAsString();20. if(resp.indexOf(jack)=-1)System.out.println(resp);21. 22. method.releaseConnection();23. pool.returnObject(hc);24. catch(Exceptione)25. e.printStackTrace();26. 27. 28. 29. );TIP =HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()HttpURLConnection1、new URL(http:/xx.xx).openConnection(); 会打开 tocol.http.HttpURLConnection, 其conn.connect()函数会从.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接, 对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient2、HttpURLConnection.getInputStream()会返回 tocol.http.HttpURLConnection$HttpInputStream,其 connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。HttpClient httpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。connectManager分为:SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,keyhostConfig value = connectPool,每次根据host从池子中获取连接,并复用之DummyConnectionManager 则不管host,一律重新建立连接每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:javaview plaincopy 1. publicclassPoolableHttpClientFactoryimplementsPoolableObjectFactory2. 3. privateinttimeout;4. publicPoolableHttpClientFactory(inttimeout)5. this.timeout=timeout;6. 7. 8. publicObjectmakeObject()throwsException9. HttpClienthttpClient=newHttpClient();10. HttpConnectionManagerParamsconfigParams=httpClient.getHttpConnectionManager().getParams();11. configParams.setConnectionTimeout(timeout);12. configParams.setSoTimeout(timeout);13. httpClient.getParams().setConnectionManagerTimeout(timeout);14. returnhttpClient;15. 16. 17. publicvoiddestroyObject(Objectobj)throwsException18. 19. 20. publicbooleanvalidateObject(Objectobj)21. returntrue;22. 23. 24. publicvoidactivateObject(Objectobj)throwsException25. 26. 27. publicvoidpassivateObject(Objectobj)throwsException28. 29. 30. javaview plaincopy 1. ExecutorServices=Executors.newFixedThreadPool(2);2. 3. finalObjectPoolpool=newGenericObjectPool(4. newPoolableHttpClientFactory(2000),5. 10,6. GenericObjectPool.WHEN_EXHAUSTED_FAIL,7. 3000,2,1,false,false,60000,10,60000,false);8. 9. for(inti=0;i2;+i)10. s.execute(newRunnable()11. publicvoidrun()12. while(true)13. try14. Thread.sleep(newRandom().nextInt(500);15. 16. PostMethodmethod=newPostMethod(:8080/index);17. HttpClienthc=(HttpClient)pool.borrowObject();18. hc.executeMethod(method);19. Stringresp=method.getResponseBodyAsString();20. if(resp.indexOf(jack)=-1)System.out.println(resp);21. 22. method.releaseConnection();23. pool.returnObject(hc);24. catch(Exceptione)25. e.printStackTrace();26. 27. 28. 29. );TIP =HttpMethod.setParams(HttpMethodParams)指的是“超时时间”这样的连接属性对于请求参数,PostMethod.addParameter(k,v) 而GetMethod则需要自行组装url了,记得作 URLEncoder.encode()HttpURLConnection1、new URL(http:/xx.xx).openConnection(); 会打开 tocol.http.HttpURLConnection, 其conn.connect()函数会从.www.http.HttpClient内部的静态连接缓冲池中获取HttpClient连接, 对应到一个Socket连接。静态连接池是一个HashTable, key = URL, value = HttpClient2、HttpURLConnection.getInputStream()会返回 tocol.http.HttpURLConnection$HttpInputStream,其 connection.getInputStream.close()函数并不关闭Socket,而是将连接还给连接池。HttpClienthttpClient.execute(Method) 直接发送请求并读取响应,而 method.getResponseBody 其实只是从response缓存中进行读取HttpClient委托HttpConnectionManager管理连接,委托HttpMethodDirector执行方法,其本身是无状态线程安全的。connectManager分为:SimpleHttpConnectorManager为默认选项。会复用连接,但是如果host改变了,则会打开新的链接。MultiThreadedHttpConnectionManager 为每个host作了一个连接池,放在map中,keyhostConfig value = connectPool,每次根据host从池子中获取连接,并复用之DummyConnectionManager 则不管host,一律重新建立连接每次new HttpClient()会新建socket,可以通过 commons-pool,自行实现连接池:javaview plaincopy 1. publicclassPoolableHttpClientFactoryimplementsPoolableObjectFactory2. 3. privateinttimeout;4. publicPoolableHttpClientFactory(inttimeout)5. this.timeout=timeout;6. 7. 8. publicObjectmakeObject()throwsException9. HttpClienthttpClient=newHttpClient();10. HttpConnectionMa

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论