2017-04-10 34 views
1

我可以通过InetAddress像上面发送一个Http GET请求。如何用HtmlUnit中的inetAddress发送请求?

HttpParams params = new BasicHttpParams(); 
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName(interfaceIp)); 
HttpClient httpClient = new DefaultHttpClient(params); 
HttpGet httpGet = new HttpGet(baseUrl); 
response = httpClient.execute(httpGet); 

但我想通过HtmlUnit尝试。有没有办法呢?

编辑:

我试试这个:

WebClient webClient = new WebClient(); 
HtmlPage page = webClient.getPage(baseurl); 
page.getTitleText(); 
+0

看到这个http://stackoverflow.com/questions/9354099/how-to -get-a-html-page-using-htmlunit –

+0

谢谢,但我不试试这个,我不得不用特定的接口ip发送请求。 –

回答

1

您可以使用:

InetAddress inetAddress = InetAddress.getByName(interfaceIp); 
webClient.getOptions().setLocalAddress(inetAddress); 
+0

我使用htmlUnit 2.23版本,我得到一个错误 - >没有setLocalAddress选项 –

+0

请更新为'2.26' –

+0

非常感谢,没关系! –