用什么替换已弃用的 `DefaultHttpAsyncClient`?

huangapple go评论131阅读模式
英文:

What to replace with deprecated `DefaultHttpAsyncClient`?

问题

我正在修改一个现有项目,但是我看到 DefaultAsyncHttpClient 被弃用了。用什么来替换这个被弃用的?

  1. HttpAsyncClient httpclient;
  2. InputStream is = null;
  3. try {
  4. // TODO: 已被弃用 ??
  5. httpclient = new DefaultHttpAsyncClient();
  6. httpclient.start();
  7. try {
  8. // 一些代码
  9. httpclient.shutdown();
  10. }
  11. catch(Exception e) {
  12. }
  13. }

.start() 方法我也无法获取,.shutdown 方法也一样无法获取。

谢谢帮助!

英文:

I'm modifying a existing project but I see that DefaultAsyncHttpClient is deprecated. What to replace the deprecated one?

  1. HttpAsyncClient httpclient;
  2. InputStream is = null;
  3. try {
  4. // TODO: deprecated ??
  5. httpclient = new DefaultHttpAsyncClient();
  6. httpclient.start();
  7. try {
  8. // some code
  9. httpclient.shutdown();
  10. }
  11. catch(Exception e) {
  12. }
  13. }

.start() i cant fetch that method either and neither i can fetch .shutdown method.

Thank for help!

答案1

得分: 0

你可以使用 CloseableHttpAsyncClient,用 close 替代 shutdown

CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
httpclient.start();
HttpGet request = new HttpGet("http://httpbin.org/get");
Future future = httpclient.execute(request, null);
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
System.out.println("Shutting down");
} finally {
httpclient.close();
}

英文:

You can use CloseableHttpAsyncClient , close instead of shutdown

> CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
> try {
> httpclient.start();
> HttpGet request = new HttpGet("http://httpbin.org/get");
> Future<HttpResponse> future = httpclient.execute(request, null);
> HttpResponse response = future.get();
> System.out.println("Response: " + response.getStatusLine());
> System.out.println("Shutting down");
> } finally {
> httpclient.close();
> }

huangapple
  • 本文由 发表于 2020年6月29日 16:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/62634056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定