当然,这里只是根据我的错误具体说一下解决办法,情况与我有不同的爱莫能助了。
public List<String> getUsernameFromHtml()
{
List<String> list=null;
HttpGet hg=new HttpGet("http://www.sina.com/PUB/foot.aspx");
try {
hg.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36" );
hg.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
hg.setHeader( "Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
hg.setConfig(requestConfig); //设置超时
CloseableHttpResponse response=httpclient.execute(hg);
System.out.println(response.getStatusLine());
HttpEntity entity=response.getEntity();
HtmlUtils hu=new HtmlUtils();
list=hu.getHtmlContentsByidName(EntityUtils.toString(entity), "lbl_userinfo");
//这一串字符还需要用htmlparse进行解析哦
}catch (Exception e) {
e.printStackTrace();
}
return list;
}
这段代码其实是没有任何问题的,所有的变量我都调试过,都有值,
但是就是执行到httpclient.execute()的时候就是不执行,我猜想是不是因为httpclient实例已经没有了,是不是已经被java的回收器回收了。
于是我就从新new 了一个,放了进去,将原来的cookie也放进去,就可以做到同一个cookie访问了
修改后添加代码为
CloseableHttpClient httpclient2 = HttpClients.custom()
.setDefaultCookieStore(cos)
.build();
——-后之览者,或将有感于斯文