把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);
}
發(fā)請求給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)
發(fā)請求給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在發(fā)送http請求前增加選項:
HttpUnitOptions.setExceptionsThrownOnscrīptError(false);
即解決。