gpt4 book ai didi

java - 关闭 HttpClient 版本 4.0.3

转载 作者:行者123 更新时间:2023-12-02 02:07:24 25 4
gpt4 key购买 nike

我有一个商业应用程序的扩展,它与 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com