在ssm里面使用webClient报错 每次启动一切正常然后用着用着客户端就开始报各种奇葩错误 一开始以为是爬取网站有问题,每次在自己机子上面跑的时候就没有问题 实在是不知道怎么回事 突然想起了spring的@bean是单例模式恍然大悟应该是一个时间段多个客户端同时访问造成的 “webclient已经关闭了页面但是还有客户端正在请求”然后我把webcilent从spring里面分离出来 用静态工厂模式的方法进行调用 果然不在报错了
代码如下
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ImmediateRefreshHandler;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
public class Web {
public static WebClient getWebClient() throws IOException {
SoftReference<WebClient> soft=new SoftReference<WebClient>(new WebClient(BrowserVersion.CHROME));
WebClient webclient = soft.get();
WebClient webclient = new WebClient(BrowserVersion.CHROME);
webclient.getOptions().setJavaScriptEnabled(true);
webclient.getOptions().setThrowExceptionOnScriptError(false);
webclient.getOptions().setCssEnabled(false);
webclient.getCookieManager().clearCookies();
webclient.getCache().clear();
webclient.setRefreshHandler(new ImmediateRefreshHandler());
webclient.getOptions().setTimeout(6000*10000);
webclient.setJavaScriptTimeout(6000*10000);
webclient.setAjaxController(new NicelyResynchronizingAjaxController());
webclient.getOptions().setJavaScriptEnabled(true);
webclient.setJavaScriptTimeout(6000*10000);
webclient.getOptions().setRedirectEnabled(true);
webclient.waitForBackgroundJavaScript(6000*10000);
webclient.getOptions().setThrowExceptionOnScriptError(false);
webclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webclient.getOptions().setUseInsecureSSL(true);
return webclient;
}
}
网友评论