关闭HttpClient版本4.0.3

时间:2018-05-28 08:26:38

标签: java windows macos apache http

我对与httpclient 4.0.3版捆绑在一起的商业应用程序进行了扩展。如果我尝试使用更高版本,则扩展程序不起作用。如果我在我的代码中使用版本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相关的内容。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

如果要在4.0.3中立即关闭所有连接,请致电client.getConnectionManager().shutdown();。请注意,您应该使用Apache HttpClient的更新版本4.5.5,因为您使用的版本是8年。