java - 关闭 HttpClient 版本 4.0.3

标签 java windows macos apache http

我有一个商业应用程序的扩展,它与 4.0.3 版本的 httpclient 捆绑在一起。如果我尝试使用更高版本,则扩展将不起作用。如果我在代码中使用 4.0.3 版本,那就可以了。我希望关闭 HttpClient,但找不到执行此操作的机制。

我的顶级类是 SwingWorker,它根据所访问的 URL 由各种子类扩展。

public class GetPrices extends SwingWorker<List<StockPrice>,Integer> {
private HttpClient client = new DefaultHttpClient();
List<StockPrice> lstStock;
MRBDebug objDebug = MRBDebug.getInstance();
public GetPrices (final List<StockPrice> lstStockp) {
    lstStock = lstStockp;
}  
@Override
protected List<StockPrice> doInBackground() throws Exception {
    int iValue = lstStock.size();
    int iProgress = 0;
    int iPrevious = 0;
    for (StockPrice spLine : lstStock) {
        try { 
            GetPrices.failIfInterrupted();
            getData(client, spLine);
            iProgress++;
        }
        catch (IOException a) {
            iProgress++;
        }
        catch (InterruptedException a) {
            iProgress++;            
        }
        int iTemp =iProgress * 100 / iValue;
        setProgress( iTemp);
    }
    return lstStock;
}

// Can safely update the GUI from this method.
@Override
protected void done() {

    List<StockPrice> spListp;
    try {
        // Retrieve the return value of doInBackground.
        lstStock = get();
        setProgress(100);
    } catch (InterruptedException e) {
        // This is thrown if the thread's interrupted.
    } catch (ExecutionException e) {
        // This is thrown if we throw an exception
        // from doInBackground.
    }
}

子类之一是:

public class GetYahooPrices extends GetPrices{
private MRBDebug objDebug; 
public GetYahooPrices (List<StockPrice> lstStock) {
    super(lstStock);
}
@Override
public void getData(HttpClient client, StockPrice spPrice) throws IOException {
    objDebug = MRBDebug.getInstance();
    String strStock = spPrice.getTicker();
    try {
        URI uri = new URL("https://finance.yahoo.com/quote/" + strStock + "?p=" + strStock).toURI();
        HttpGet httpGet = new HttpGet(uri);
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        InputStream stream = entity.getContent();

我希望在完成对 URL 的所有调用后关闭 HttpCLient,但在版本 4.0.3 中找不到任何执行此操作的内容。谁能指出我正确的方向?

最佳答案

如果你想在4.0.3中立即关闭所有连接,只需调用client.getConnectionManager().shutdown();即可。请注意,您应该使用 Apache HttpClient 的更新版本 4.5.5,因为您使用的版本已有 8 年历史。

关于java - 关闭 HttpClient 版本 4.0.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50562072/

相关文章:

java - print() 方法未打印正确的 ArrayList 内容

java - 用于 Android 开发的 Light IDE

macos - 获取 OS X 上的型号标识符字符串

java - Windows 10下Java Apps放大问题

java - 如何从 Java Web 应用程序发送 SMS 消息

c - 如何显示来自驱动程序的弹出消息框(内核模式)?

windows - 使用 windbg 转储 EPROCESS

objective-c - 菜单项已启用,但仍显示为灰色

linux - 有没有办法通过 Bash 在 find 命令中指定排除的异常(exception)?

java - 为什么 CSVParser 正在读取下一个 CSVRecord