Aaron,
Let me know what you think about it. Your feedback will be appreciated. If find this
demo all right, I'll commit it to the CVS
Cheers
Oleg
===========================================================================================
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.*;
public class FormLoginDemo
{
static final String LOGON_SITE = "developer.java.sun.com";
static final int LOGON_PORT = 80;
public FormLoginDemo() {
super();
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
// 'developer.java.sun.com' has cookie compliance problems
// Their session cookie's domain attribute is in violation of the RFC2109
// We have to resort to using compatibility cookie policy
client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
GetMethod authget = new GetMethod("/servlet/SessionServlet");
client.executeMethod(authget);
System.out.println("Login form get: " + authget.getStatusLine().toString());
// release any connection resources used by the method
authget.releaseConnection();
// See if we got any cookies
Cookie[] initcookies =
client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/", false);
System.out.println("Initial set of cookies:");
if (initcookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < initcookies.length; i++) {
System.out.println("- " + initcookies[i].toString());
}
}
PostMethod authpost = new PostMethod("/servlet/SessionServlet");
// Prepare login parameters
NameValuePair action = new NameValuePair("action", "login");
NameValuePair url = new NameValuePair("url", "/index.html");
NameValuePair userid = new NameValuePair("UserId", "myid");
NameValuePair password = new NameValuePair("Password", "xxxxx");
authpost.setRequestBody(
new NameValuePair[] {action, url, userid, password});
client.executeMethod(authpost);
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();
// See if we got any new cookies
// ==========================
// The only way of telling whether logon succeeded is
// by finding a session cookie
Cookie[] logoncookies =
client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/", false);
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < logoncookies.length; i++) {
System.out.println("- " + logoncookies[i].toString());
}
}
}
}
===================================================================================
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Dienstag, 18. M�rz 2003 20:26
To: [EMAIL PROTECTED]
Subject: DO NOT REPLY [Bug 10817] - Provide more Example Code
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10817>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10817
Provide more Example Code
------- Additional Comments From [EMAIL PROTECTED] 2003-03-18 19:26 -------
I would like to see an example that connects to a page (i.e.
http://cgi1.ebay.com/aw-cgi/eBayISAPI.dll?MyEbayLogin ) download the login page
and cookies, submit the form back to the server, and get the resulting page.
No graphical client, no swing libraries, just a simple connect, get the page,
submit the page, get the result.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]