Wednesday, February 8, 2012

Form-based authentication with Apache HttpClient

Just a Java code snippet which logins into a web server with form-based authentication using Apache HttpClient (version 4.x)
String serverUrl = "http://localhost:8080/myserver";
HttpPost httpPost = new HttpPost(serverUrl + "/j_security_check");
List<BasicNameValuePair> = Arrays.asList(
 new BasicNameValuePair("j_username", "username"),
 new BasicNameValuePair("j_password", "password"));
httpPost.setEntity(new UrlEncodedFormEntity(valuePairs, HTTP.UTF_8));
httpClient.execute(httpPost);

0 comments:

Post a Comment