• 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

  • 相关阅读:
    js 字符串中提取ip地址
    echart lengend 选中事件
    反射与注解
    clientX、clientY、offsetLeft、offsetTop、offsetWidth、offsetHeight
    图片放大和缩小
    拖拽文字辅助线对齐
    文字随着鼠标移动而移动(文字拖拽移动)
    Java 数组转 List 的三种方式及使用场景
    【Docker(二)】Docker镜像、容器、仓库命令详解
    【Docker(一)】走进Docker的第一步
  • 原文地址:https://www.cnblogs.com/sunxucool/p/2850647.html
Copyright © 2020-2023  润新知