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

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

What to replace with deprecated `DefaultHttpAsyncClient`?

问题

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

HttpAsyncClient httpclient;
InputStream is = null;
try {
    // TODO: 已被弃用 ??
    httpclient = new DefaultHttpAsyncClient();
    httpclient.start();
    try {

        // 一些代码

        httpclient.shutdown();
    }

    catch(Exception e) {

    }
}

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

谢谢帮助!

英文:

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

		HttpAsyncClient httpclient;
	    InputStream is = null;
	    try {
		    // TODO: deprecated ??
		    httpclient = new DefaultHttpAsyncClient();
		    httpclient.start();
        try {
           
            // some code

            httpclient.shutdown();
        }
        
        catch(Exception e) {
         
        }
        }

.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:

确定