【问题标题】:Closing HttpClient version 4.0.3关闭 HttpClient 版本 4.0.3
【发布时间】:2018-05-28 08:26:38
【问题描述】:

我有一个与 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 版中找不到任何执行此操作的内容。谁能指出我正确的方向?

【问题讨论】:

    标签: java windows macos apache http


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,正如我在帖子中所说,不幸的是,我正在使用的应用程序使用 4.0.3,我已要求他们升级,但他们提供了 JRE 和 jar,所以我被卡住了,直到他们升级。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多