从DefaultHttpClient以优雅方式退出

时间:2017-07-19 18:41:42

标签: java http-post httpclient

如何退出HttpClient?找不到方法。

HttpClient Client = new DefaultHttpClient();
String userpass = usr + ":" + pwd;

HttpPost httpGet = new HttpPost(dataimport_cmd);
String encoding = DatatypeConverter.printBase64Binary(userpass.getBytes("UTF-8"));
httpGet.setHeader("Authorization", "Basic " + encoding);

Client.execute(httpGet);

我尝试将执行封闭到closeablehttpresponse,但它没有从类中退出。

CloseableHttpResponse response = (CloseableHttpResponse) 
Client.execute(httpGet);
response.close();

我也尝试过使用return来退出课程。

返回;

1 个答案:

答案 0 :(得分:0)

如果您的http客户端版本高于4.3,则可以使用CloseableHttpClient而不是HttpClient。它有一个close()方法。

CloseableHttpClient client = new DefaultHttpClient();
...
client.close();