java 取消 HttpClient 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5400258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:56:25  来源:igfitidea点击:

Cancel an HttpClient request

javaapache-httpclient-4.x

提问by Ben Kulbertis

I am making a simple request for a file using HttpClient from the Apache Commons. Here is my current code:

我正在使用 Apache Commons 中的 HttpClient 对文件进行简单请求。这是我当前的代码:

    httpclient = new DefaultHttpClient();
    httpget = new HttpGet(location);
    context = new BasicHttpContext();
    response = httpclient.execute(httpget, context);
    entity = response.getEntity();

What would I need to do to cancel this request in the middle of the download?

我需要怎么做才能在下载过程中取消这个请求?

回答by Suresh Kumar

You can use httpget.abort()method to abort the request mid way.

您可以使用httpget.abort()方法中止请求。

See example here

请参阅此处的示例