httpClient释放链接的问题

发布于 2021-12-03 14:59:46 字数 1627 浏览 869 评论 17

昨天接触到了httpclient和jsoup,

使用httpclient模拟登陆OA,想抓OA的数据下来,无实在意义,只是练手。

不过在过程中出了一点问题。

public static boolean login(){
		HttpGet get = new HttpGet(uri+"/logincheck.php");
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//....
		try {
			String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
			get.setURI(new URI(get.getURI()+"?"+str));
			response = client.execute(get);
			//....
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(response.getStatusLine().getStatusCode()==200){
			System.out.println("登陆成功!");
			return true;
		}else{
			return false;
		}
	}
这个方法没有问题,不过在登陆之后,我直接访问一个OA里面的新闻页面,去抓数据的时候就会出错。

public static String getNewsContent(){
		HttpGet get = new HttpGet(uri+"/general/notify/show");
		try {
			response = client.execute(get);
			HttpEntity entity = response.getEntity();
			String newsContent = EntityUtils.toString(entity);
			Document doc = Jsoup.parse(newsContent);
			Elements elements = doc.getElementsByClass("TableLine1");
			for(Element element : elements){
				System.out.println(element.val());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
在第二次client.execute(get)的时候就会报错。

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

狼亦尘 2021-12-08 11:33:19

http请求都是分开请求的,但是两个http请求可以共用一个Socket连接

你需要的解决的是,怎么在第二次请求的时候,带上第一次请求返回的sessionid

夜司空 2021-12-08 11:33:15

请求结束后调用get.abort或者EntityUtils.consume试试

凌乱心跳 2021-12-08 11:33:15

我的意思是在 第一次请求结束后,怎么保持登录状态进行第二次请求。 释放连接,第二次请求的时候又没登录, 不释放连接,就又报上面的错误。 就像用户登录了之后进行操作一样。

怎言笑 2021-12-08 11:33:15

感谢答主,碰到同样的问题,我这边关闭response就行了

兮颜 2021-12-08 11:33:12

那怎么让程序记住自己的登录状态,继而去请求下一个需要登录才能请求的页面呢

为你鎻心 2021-12-08 11:33:12

回复
两个请求就得用两个连接,如何连接的是httpclient封装在内部的,外部不做此控制,就像你用普通jsp请求一样,你面向的是一次次的请求,而不是连接,而且每次请求必须释放,不然端口一直占用着

鹤舞 2021-12-08 11:33:12

我的意思是在 第一次请求结束后,怎么保持登录状态进行第二次请求。 释放连接,第二次请求的时候又没登录, 不释放连接,就又报上面的错误。 就像用户登录了之后进行操作一样。

琴流音 2021-12-08 11:33:10

提示很明确,你没有释放连接,我在官方的Samples里面找到一段代码应该可以帮助你

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
	HttpGet httpget = new HttpGet("http://www.apache.org/");
	// Execute HTTP request
	CloseableHttpResponse response = httpclient.execute(httpget);
	try {
		// Get hold of the response entity
		HttpEntity entity = response.getEntity();
		// If the response does not enclose an entity, there is no need
		// to bother about connection release
		if (entity != null) {
			InputStream instream = entity.getContent();
			try {
				instream.read();
				// do something useful with the response
			} catch (IOException ex) {
				// In case of an IOException the connection will be released
				// back to the connection manager automatically
				throw ex;
			} finally {
				// Closing the input stream will trigger connection release
				instream.close();
			}
		}
	} finally {
		response.close();
	}
} finally {
	httpclient.close();
}

眼眸 2021-12-08 11:33:01

我的意思是在 第一次请求结束后,怎么保持登录状态进行第二次请求。 释放连接,第二次请求的时候又没登录, 不释放连接,就又报上面的错误。 就像用户登录了之后进行操作一样。

明媚如初 2021-12-08 11:18:31

回复
consume又不是释放连接,销毁你打开的entity而已,你的client还是那个就行

眼泪淡了忧伤 2021-12-08 10:59:49

不知道你用的哪个版本,4.x之后改动挺多的,和你这个问题相关的比如PoolingClientConnectionManager代替了ThreadSafeClientConnManager、EntityUtils.consume代替了consumeContent
,用连接池维护或者每次打开entity之后consume

一人独醉 2021-12-08 10:51:45

我的意思是在 第一次请求结束后,怎么保持登录状态进行第二次请求。 就像用户登录了之后进行操作一样。

拍不死你 2021-12-08 06:55:02

回复
用发登录请求的HttpClient对象再次请求就行啊

残花月 2021-12-08 00:52:39

不行的,会报错,就是上面的那个错误。 可以看我上面的代码,就是你说的那样

挽清梦 2021-12-07 01:48:16

请求结束后调用get.abort或者EntityUtils.consume试试

霞映澄塘 2021-12-06 12:09:50

请求结束后调用get.abort或者EntityUtils.consume试试

墨洒年华 2021-12-04 00:31:38

引用来自“lietome”的答案

提示很明确,你没有释放连接,我在官方的Samples里面找到一段代码应该可以帮助你

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
	HttpGet httpget = new HttpGet("http://www.apache.org/");
	// Execute HTTP request
	CloseableHttpResponse response = httpclient.execute(httpget);
	try {
		// Get hold of the response entity
		HttpEntity entity = response.getEntity();
		// If the response does not enclose an entity, there is no need
		// to bother about connection release
		if (entity != null) {
			InputStream instream = entity.getContent();
			try {
				instream.read();
				// do something useful with the response
			} catch (IOException ex) {
				// In case of an IOException the connection will be released
				// back to the connection manager automatically
				throw ex;
			} finally {
				// Closing the input stream will trigger connection release
				instream.close();
			}
		}
	} finally {
		response.close();
	}
} finally {
	httpclient.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文