HtmlUnit communication with the remote server is based on underlying HttpClient, and the latter allows to access web sites which require authentication by providing credentials programmatically. Is it possible to do the same thing with HtmlUnit without going through the login form and its elements?
Is it possible to authenticate with HtmlUnit by supplying credentials without dealing with the form?
Asked
Active
Viewed 651 times
2 Answers
1
You can use
webRequest.setAdditionalHeader() or webClient.addRequestHeader()

Ahmed Ashour
- 5,179
- 10
- 35
- 56
0
If I understand your question, you want to do a request without simulate form click? You can do login with a POST method using WebRequest API
private HtmlPage login(String name, String pass) {
WebRequest requestSettings = new WebRequest(new URL(myUrlLoginString), HttpMethod.POST);
// Set the request parameters
ArrayList<NameValuePair> requestParameters = new ArrayList<NameValuePair>();
requestParameters.add(new NameValuePair("login", name));
requestParameters.add(new NameValuePair("password", pass));
requestParameters.add(new NameValuePair("x", "66"));
requestParameters.add(new NameValuePair("y", "28"));
requestSettings.setRequestParameters(requestParameters);
Page page = webClient.getPage(requestSettings);
return (HtmlPage) page;
}

Adrien
- 365
- 3
- 9