从DefaultHttpClient以优雅退出
如何从HttpClient退出?没有找到方法。从DefaultHttpClient以优雅退出
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;
如果您的http客户端版本高于4.3,您可以使用CloseableHttpClient而不是HttpClient。它有一个close()方法。
CloseableHttpClient client = new DefaultHttpClient();
...
client.close();
谢谢。其实我采用这种方法,但它仍然没有退出。虽然我没有使用defaulthttpclient,因为之前的评论建议不要使用depracated类。 CloseableHttpClient客户端= HttpClientBuilder.create()。build(); \t HttpPost httpGet = new HttpPost(dataimport_cmd); \t String encoding = DatatypeConverter.printBase64Binary(userpass.getBytes(“UTF-8”)); \t httpGet.setHeader(“Authorization”,“Basic”+ encoding); \t \t client.execute(httpGet); \t \t client.close(); – user3286012
其实你的源代码是正确的,为我工作。但是,如果我使用像“http://192.168.3.66:80”这样的URL,其中192.168.3.66处于关闭状态,则应用程序正在等待client.execute()调用,直到超时。你可能有类似的情况。 – taner
你为什么使用已弃用的类?对于已弃用的问题,您不会找到太多帮助 –
hmmmm。通常我使用的IDE会指示它。让我检查一下新班 – user3286012