Ich habe ein kleines Problem mit einem FormLogin. Ich möchte mich mittels HttpClient in das Confixx-System meiner Homepage einloggen um dort einige Sachen zu erledigen.
Dazu benutze ich folgenden Code
ist das Bsp von jakarta, was ich einfach umgeschrieben habe. Einloggen kann ich mich, ich bekomme auch eine Server-ID, nur beim redirect bekomme ich einen Bad Request. Folgendes wird ausgegeben
Login form get: HTTP/1.1 200 OK
Initial set of cookies:
- SID=d79978fe65016fef5afcaadbfe650f5b
Login form post: HTTP/1.1 302 Found
Logon cookies:
- SID=d79978fe65016fef5afcaadbfe650f5b
- server_id=0d1a0dca9569989047ff1fece438c8cb
Redirect target: user/web1/
Redirect: HTTP/1.1 400 Bad Request
Es werden folgende Header gesendet, wenn ich den BadRequest bekomme
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "GET user/web1/index.php HTTP/1.1[\r][\n]"
2007/09/17 20:52:56:827 CEST [DEBUG] HttpMethodBase - Adding Host request header
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "Host: login3.internet4ever.de[\r][\n]"
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "[\r][\n]"
2007/09/17 20:52:56:952 CEST [DEBUG] header - << "HTTP/1.1 400 Bad Request[\r][\n]"
2007/09/17 20:52:56:952 CEST [DEBUG] header - << "HTTP/1.1 400 Bad Request[\r][\n]"
2007/09/17 20:52:56:952 CEST [DEBUG] header - << "Date: Mon, 17 Sep 2007 18:59:57 GMT[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch4 proxy_html/2.5 mod_ssl/2.2.3 OpenSSL/0.9.8c mod_perl/2.0.2 Perl/v5.8.8[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Vary: accept-language,accept-charset[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Accept-Ranges: bytes[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Connection: close[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Content-Type: text/html; charset=iso-8859-1[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Content-Language: en[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] HttpMethodBase - Should close connection in response to directive: close
2007/09/17 20:52:56:967 CEST [DEBUG] HttpConnection - Releasing connection back to connection manager.Redirect: HTTP/1.1 400 Bad Request
jemand ne Idee, woran das liegen könnte?
Dazu benutze ich folgenden Code
Code:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.*;
/**
*
* A example that demonstrates how HttpClient APIs can be used to perform
* form-based logon.
* </p>
*
* @author Oleg Kalnichevski
*
*/
public class test2
{
static final String LOGON_SITE = "login3.internet4ever.de";
static final int LOGON_PORT = 80;
public test2() {
super();
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
//client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// '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
GetMethod authget = new GetMethod("/user/index.php");
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
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] initcookies = cookiespec.match(
LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
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("/login.php");
// Prepare login parameters
NameValuePair action = new NameValuePair("action", "lgtogin");
NameValuePair url = new NameValuePair("url", "/index.php");
NameValuePair type = new NameValuePair("Content-Type","application/x-www-form-urlencoded");
NameValuePair userid = new NameValuePair("username", "user");
NameValuePair password = new NameValuePair("password", "passwort");
authpost.setRequestBody(
new NameValuePair[] {url, userid, password,type,action});
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 cookies
// The only way of telling whether logon succeeded is
// by finding a session cookie
Cookie[] logoncookies = cookiespec.match(
LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
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());
}
}
// Usually a successful form-based login results in a redicrect to
// another url
int statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
//newuri+="index.php";
System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(redirect);
//System.out.println(neu.getResponseBodyAsString());
System.out.println("Redirect: " + redirect.getStatusLine().toString());
//System.out.println(redirect.getResponseBodyAsString());
// release any connection resources used by the method
redirect.releaseConnection();
} else {
System.out.println("Invalid redirect");
System.exit(1);
}
}
}
}
ist das Bsp von jakarta, was ich einfach umgeschrieben habe. Einloggen kann ich mich, ich bekomme auch eine Server-ID, nur beim redirect bekomme ich einen Bad Request. Folgendes wird ausgegeben
Login form get: HTTP/1.1 200 OK
Initial set of cookies:
- SID=d79978fe65016fef5afcaadbfe650f5b
Login form post: HTTP/1.1 302 Found
Logon cookies:
- SID=d79978fe65016fef5afcaadbfe650f5b
- server_id=0d1a0dca9569989047ff1fece438c8cb
Redirect target: user/web1/
Redirect: HTTP/1.1 400 Bad Request
Es werden folgende Header gesendet, wenn ich den BadRequest bekomme
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "GET user/web1/index.php HTTP/1.1[\r][\n]"
2007/09/17 20:52:56:827 CEST [DEBUG] HttpMethodBase - Adding Host request header
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "Host: login3.internet4ever.de[\r][\n]"
2007/09/17 20:52:56:827 CEST [DEBUG] header - >> "[\r][\n]"
2007/09/17 20:52:56:952 CEST [DEBUG] header - << "HTTP/1.1 400 Bad Request[\r][\n]"
2007/09/17 20:52:56:952 CEST [DEBUG] header - << "HTTP/1.1 400 Bad Request[\r][\n]"
2007/09/17 20:52:56:952 CEST [DEBUG] header - << "Date: Mon, 17 Sep 2007 18:59:57 GMT[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch4 proxy_html/2.5 mod_ssl/2.2.3 OpenSSL/0.9.8c mod_perl/2.0.2 Perl/v5.8.8[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Vary: accept-language,accept-charset[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Accept-Ranges: bytes[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Connection: close[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Content-Type: text/html; charset=iso-8859-1[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "Content-Language: en[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] header - << "[\r][\n]"
2007/09/17 20:52:56:967 CEST [DEBUG] HttpMethodBase - Should close connection in response to directive: close
2007/09/17 20:52:56:967 CEST [DEBUG] HttpConnection - Releasing connection back to connection manager.Redirect: HTTP/1.1 400 Bad Request
jemand ne Idee, woran das liegen könnte?