Hi Roland,
Thanks for your answer.
Have you studied the Client HTTP Programming Primer?
http://wiki.apache.org/jakarta-httpclient/ForAbsoluteBeginners
Thanks Roland
Yep. But this doesn't help me much. The tutorial doesn't desribe how to
automate the j_security_check - form-based authentication. There ready
to use schemas for BasicAuth, Digest and so on but no j_security-check
form-based authentication :-(.
I found some code snipples in the net
(http://forum.java.sun.com/thread.jspa?threadID=546542&messageID=4154990)
and I have builded a program that does the thing that I need.
Here is the code that works:
------------------------------------------------------BEGIN
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class ClientAutoAuthentication {
public static void main(String[] args) throws HttpException,
IOException {
String strURL = "http://localhost:8080/test-form-auth/test.jsp";
System.out.println("Target URL: " + strURL);
HttpState initialState = new HttpState();
HttpClient httpclient = new HttpClient();
httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
httpclient.setState(initialState);
// RFC 2101 cookie management spec is used per default
// to parse, validate, format & match cookies
httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
GetMethod httpget = new GetMethod(strURL);
int result = httpclient.executeMethod(httpget);
System.out.println("Response status code: " + result);
Cookie[] cookies = httpclient.getState().getCookies();
System.out.println("Present cookies: ");
for (int i = 0; i < cookies.length; i++) {
System.out.println(" - " + cookies[i].toExternalForm());
}
httpget.releaseConnection();
PostMethod postMethod = new
PostMethod(("http://localhost:8080/test-form-auth/j_security_check"));
NameValuePair[] postData = new NameValuePair[3];
postData[0] = new NameValuePair("j_username", "test");
postData[1] = new NameValuePair("j_password", "test");
postData[2] = new NameValuePair("Login", "login");
postMethod.addParameters(postData);
for(int i = 0; i < cookies.length; i++){
initialState.addCookie(cookies[i]);
}
httpclient.setState(initialState);
try {
httpclient.executeMethod(postMethod);
} catch (HttpException httpe) {
System.err.print("HttpException");
System.err.println(httpe.getMessage());
httpe.printStackTrace();
} catch (IOException ioe) {
System.err.print("IOException");
System.err.println(ioe.getMessage());
ioe.printStackTrace();
}
postMethod.releaseConnection();
GetMethod getMethod = new
GetMethod("http://localhost:8080/test-form-auth/test.jsp");
try {
httpclient.executeMethod(getMethod);
} catch (HttpException httpe) {
System.out.println(httpe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
String responseBody = getMethod.getResponseBodyAsString();
System.out.println(responseBody);
getMethod.releaseConnection();
}
}
-----------------------------------------------------------END
The only problem is that, I need to integrate this programm in a
servlet. This is quite different and very frustrating :-(.
Have anybody an idey how to integrate this in a servlet context ?
-- Franck
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]