昨天接触到了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.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
提示很明确,你没有释放连接,我在官方的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();}提示很明确,你没有释放连接,我在官方的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请求一样,你面向的是一次次的请求,而不是连接,而且每次请求必须释放,不然端口一直占用着我的意思是在第一次请求结束后,怎么保持登录状态进行第二次请求。释放连接,第二次请求的时候又没登录,不释放连接,就又报上面的错误。就像用户登录了之后进行操作一样。感谢答主,碰到同样的问题,我这边关闭response就行了
http请求都是分开请求的,但是两个http请求可以共用一个Socket连接
你需要的解决的是,怎么在第二次请求的时候,带上第一次请求返回的sessionid