zoukankan      html  css  js  c++  java
  • httclient caught when connecting to the target host:Address already in use 悟寰轩

    转自:http://seanhe.iteye.com/blog/234759

    方法一: 
    把事例代码中的第一行实例化代码改为如下即可,在method.releaseConnection();之后connection manager会关闭connection 。 

    Java代码 
    1. HttpClient client = new HttpClient(new HttpClientParams(),new SimpleHttpConnectionManager(true) );  


    方法二: 
    实例化代码使用:HttpClient client = new HttpClient(); 
    在method.releaseConnection();之后加上 

    Java代码 
    1. ((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();  


    shutdown源代码很简单,看了一目了然 

    Java代码 
    1. public void shutdown() {  
    2.     httpConnection.close();  
    3. }  


    方法三: 
    实例化代码使用:HttpClient client = new HttpClient(); 
    在method.releaseConnection();之后加上 
    client.getHttpConnectionManager().closeIdleConnections(0);此方法源码代码如下: 

    Java代码 
    1. public void closeIdleConnections(long idleTimeout) {  
    2.     long maxIdleTime = System.currentTimeMillis() - idleTimeout;  
    3.     if (idleStartTime <= maxIdleTime) {  
    4.         httpConnection.close();  
    5.     }  
    6. }  


    将idleTimeout设为0可以确保链接被关闭。 
    以上这三种方法都是有客户端主动关闭TCP链接的方法。下面再介绍由服务器端自动关闭链接的方法。 
    方法四: 
    代码实现很简单,所有代码就和最上面的事例代码一样。只需要在HttpMethod method = new GetMethod("http://www.apache.org");加上一行HTTP头的设置即可 

    Java代码 
    1. method.setRequestHeader("Connection""close");  

    看一下HTTP协议中关于这个属性的定义: 
    HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. For example, 
           Connection: close

  • 相关阅读:
    json的相关知识
    实现highcharts放大缩小
    js总结
    sql语句学习
    关于echarts生成雷达图的一些参数介绍
    在表单导航中如何判断其进行到第几步
    IDEA的相关使用-----快捷键
    简单安装squid步骤
    feign调用文件上传服务,传参MultipartFile
    java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
  • 原文地址:https://www.cnblogs.com/sunxucool/p/2850647.html
Copyright © 2011-2022 走看看