开发者社区 问答 正文

httpClient释放链接的问题?报错

昨天接触到了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.

展开
收起
爱吃鱼的程序员 2020-06-22 15:10:52 772 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    引用来自“lietome”的答案

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

    CloseableHttpClienthttpclient=HttpClients.createDefault();try{HttpGethttpget=newHttpGet("http://www.apache.org/");//ExecuteHTTPrequestCloseableHttpResponseresponse=httpclient.execute(httpget);try{//GetholdoftheresponseentityHttpEntityentity=response.getEntity();//Iftheresponsedoesnotencloseanentity,thereisnoneed//tobotheraboutconnectionreleaseif(entity!=null){InputStreaminstream=entity.getContent();try{instream.read();//dosomethingusefulwiththeresponse}catch(IOExceptionex){//IncaseofanIOExceptiontheconnectionwillbereleased//backtotheconnectionmanagerautomaticallythrowex;}finally{//Closingtheinputstreamwilltriggerconnectionreleaseinstream.close();}}}finally{response.close();}}finally{httpclient.close();}

    引用来自“徐春鹏”的答案

    引用来自“名字是什么能吃吗”的答案

    请求结束后调用get.abort或者EntityUtils.consume试试请求结束后调用get.abort或者EntityUtils.consume试试不行的,会报错,就是上面的那个错误。可以看我上面的代码,就是你说的那样回复 @nice_so:用发登录请求的HttpClient对象再次请求就行啊我的意思是在第一次请求结束后,怎么保持登录状态进行第二次请求。就像用户登录了之后进行操作一样。不知道你用的哪个版本,4.x之后改动挺多的,和你这个问题相关的比如PoolingClientConnectionManager代替了ThreadSafeClientConnManager、EntityUtils.consume代替了consumeContent ,用连接池维护或者每次打开entity之后consume 回复 @nice_so:consume又不是释放连接,销毁你打开的entity而已,你的client还是那个就行我的意思是在第一次请求结束后,怎么保持登录状态进行第二次请求。释放连接,第二次请求的时候又没登录,不释放连接,就又报上面的错误。就像用户登录了之后进行操作一样。

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

    CloseableHttpClienthttpclient=HttpClients.createDefault();try{HttpGethttpget=newHttpGet("http://www.apache.org/");//ExecuteHTTPrequestCloseableHttpResponseresponse=httpclient.execute(httpget);try{//GetholdoftheresponseentityHttpEntityentity=response.getEntity();//Iftheresponsedoesnotencloseanentity,thereisnoneed//tobotheraboutconnectionreleaseif(entity!=null){InputStreaminstream=entity.getContent();try{instream.read();//dosomethingusefulwiththeresponse}catch(IOExceptionex){//IncaseofanIOExceptiontheconnectionwillbereleased//backtotheconnectionmanagerautomaticallythrowex;}finally{//Closingtheinputstreamwilltriggerconnectionreleaseinstream.close();}}}finally{response.close();}}finally{httpclient.close();}

    那怎么让程序记住自己的登录状态,继而去请求下一个需要登录才能请求的页面呢回复 @nice_so:两个请求就得用两个连接,如何连接的是httpclient封装在内部的,外部不做此控制,就像你用普通jsp请求一样,你面向的是一次次的请求,而不是连接,而且每次请求必须释放,不然端口一直占用着我的意思是在第一次请求结束后,怎么保持登录状态进行第二次请求。释放连接,第二次请求的时候又没登录,不释放连接,就又报上面的错误。就像用户登录了之后进行操作一样。

    引用来自“名字是什么能吃吗”的答案

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

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

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

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

    2020-06-22 15:11:11
    赞同 展开评论
问答分类:
问答地址: