1.该方法支持HTTP代理访问
2.集成的代码如下,改方法用的是模拟Firefox页面
public static final String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0";
//创建Webclient成功
public void WebclientCreat(String proxyHost, int proxyPort) {
// 用http代理
webClient = new WebClient();
//设置代理
ProxyConfig proxyConfig = webClient.getOptions().getProxyConfig();
proxyConfig.setProxyHost(proxyHost);
proxyConfig.setProxyPort(proxyPort);
try {
webClient.getBrowserVersion().setUserAgent(userAgent);
webClient.setRefreshHandler(new ThreadedRefreshHandler());
//禁用Css,可避免自动二次请求CSS进行渲染
webClient.getOptions().setCssEnabled(false);
//启动js
webClient.getOptions().setJavaScriptEnabled(false);
//运行报错不抛出异常
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setRedirectEnabled(true);
//时间
webClient.getOptions().setTimeout(120000);
//忽略ssl认证
webClient.getOptions().setUseInsecureSSL(true);
webClient.setJavaScriptTimeout(300000);
} catch (Exception e) {
e.printStackTrace();
}
}
这里是在webclient创建成功之后访问页面
public void StartWebclient(){
//访问的URL,suhu
String url = "https://www.sohu.com/";
//访问URL,将html页面渲染出来
HtmlPage page = webClient.getPage(url);
//能够查看sohu的主页面源码
System.out.println(page.asXml());
}
————————————————
版权声明:本文为CSDN博主「杨冬冬」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/you_are_my_life/article/details/77525493