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

  • 相关阅读:
    RPC 调用简述
    bootstrap tab切换无效的一种情况
    html块元素和内联元素介绍
    重装系统后,Linux虚拟机无法联网
    datatables.js表头挤在一列的解决办法
    &nbsp; &ensp; &emsp; &thinsp;&zwnj;&zwj; 6种空白空格的区别
    pagination.js通过ajax请求获取数据
    谷歌浏览器调制,控制面板各命令总结
    点击表格弹窗获取另外一套数据之后,原表格相关数据的调用
    datatable columns与columnDefs
  • 原文地址:https://www.cnblogs.com/sunxucool/p/2850647.html
Copyright © 2011-2022 走看看