把httpunit/jars目录下jar都加入classpath,否则运行时出错。
wc=newWebConversation();//初始化httpunit
try{
GetMethodWebRequest req =newGetMethodWebRequest(
// "http://www.google.cn/search?h1=zh-CN&q=mp4");
"http://www.sina.com.cn");
//HttpUnitOptions.setExceptionsThrownOnscrīptError(false);
WebResponse rep =wc.getResponse(req);
assertTrue(true);
WebLink[] links=rep.getLinks();
for(inti=0;i < links.length;i++)
{
System.out.println(links[i].getURLString());
}
WebResponse n_rep=links[4].click();
System.out.println(n_rep.getText());
}catch(Exception e) {
e.printStackTrace();
assertFalse(false);
}
发请求给www.sina.com.cn异常1
org.mozilla.javascrīpt.EcmaError: TypeError: Cannot call method "toLowerCase" of undefined (httpunit
at com.meterware.httpunit.javascrīpt.scrīptingEngineImpl.handlescrīptException(scrīptingEngineImpl.java:64)
发请求给www.alibaba.com.cn异常2
at com.meterware.httpunit.javascrīpt.scrīptingEngineImpl.handlescrīptException(scrīptingEngineImpl.java:64)
at com.meterware.httpunit.javascrīpt.scrīptingEngineImpl.runscrīpt(scrīptingEngineImpl.java:95)
参考:http://lupingui.javaeye.com/blog/239738在发送http请求前增加选项:
HttpUnitOptions.setExceptionsThrownOnscrīptError(false);
即解决。