hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Problem with redirection
Date Sat, 05 Nov 2005 16:34:25 GMT
On Fri, 2005-11-04 at 20:44 +0100, Blub42@gmx.net wrote:
> Hello,
> I wrote an application that tries to log in to a https-domain by
> transmitting login and password by http-POST. As response I receive two
> cookies and a redirection code 302. Trying to handle this redirection, the
> program crashes. 
> 
> Perhaps anybody might help? Thanks a lot in advance.
> Pascal
> 

Pascal,
This will fix the problem
client.getParams().setParameter("http.protocol.single-cookie-header",
Boolean.TRUE);

Oleg

> ---------------------------------------------------------------------
> import org.apache.commons.httpclient.*;
> import org.apache.commons.httpclient.methods.*;
> import org.apache.commons.httpclient.cookie.*;
> import java.io.*;
> 
> public class myClient {
> 
> 	private static String url =
> "https://www.openbc.com/cgi-bin/user.fpl?op=home";
> 
> 	public static void main(String[] args)
> {
> 		System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
> 		System.setProperty("org.apache.commons.logging.simplelog.showdatetime","true");
> 		System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire","debug");
> 		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient","debug");
> 
> 		HttpClient client = new HttpClient();
> 
> 		PostMethod post = new PostMethod(url);
> 		NameValuePair[] data = { new NameValuePair("op", "login"),
> 				new NameValuePair("dest", "/cgi-bin/user.fpl?op=home"),
> 				new NameValuePair("login_user_name", "***"),
> 				new NameValuePair("login_password", "***")
> };
> 		post.setRequestBody(data);
> 		post.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
> 		client.getParams().setParameter("http.protocol.single-cookie-header","true");
> 		// post.setFollowRedirects(true);
> 		try {
> 			int statuscode = client.executeMethod(post);
> 			Header locationHeader = post.getResponseHeader("location");
> 
> 			Reader r = new InputStreamReader(new BufferedInputStream(post
> 					.getResponseBodyAsStream()));
> 			int c;
> 			while ((c = r.read()) != -1)
> 				System.out.print((char) c);
> 
> 			Cookie[] cookies = client.getState().getCookies();
> 			// Display the cookies
> 			System.out.println("Present cookies: ");
> 			for (int i = 0; i < cookies.length; i++)
> 				System.out.println(" - " + cookies[i].toExternalForm());
> 			post.releaseConnection();
> 
> 			// redirection *********************************************
> 			if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
> 					|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
> 					|| (statuscode == HttpStatus.SC_SEE_OTHER)
> 					|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
> 
> 				if (locationHeader != null) {
> 					String newuri = locationHeader.getValue();
> 					if ((newuri == null) || (newuri.equals(""))) {
> 						newuri = "/";
> 					}
> 					System.out.println("Redirect target: " + newuri);
> 					GetMethod redirect = new GetMethod(newuri);
> 					client.executeMethod(redirect);
> 					System.out.println("Redirect: "
> 							+ redirect.getStatusLine().toString());
> 					// release any connection resources used by the method
> 					redirect.releaseConnection();
> 				} else {
> 					System.out.println("Invalid redirect");
> 					System.exit(1);
> 				}
> 			}
> 			// *********************************************************
> 
> 		} catch (HttpException e) {
> 			System.err.println("Fatal protocol violation: " + e.getMessage());
> 			e.printStackTrace();
> 		} catch (IOException e) {
> 			System.err.println("Fatal transport error: " + e.getMessage());
> 			e.printStackTrace();
> 		}
> 
> 	}
> }
> 
> ----------------------------------------------------------------------
> ----------------------------------------------------------------------
> 2005/11/04 20:08:28:817 CET [DEBUG] HttpClient - Java version: 1.4.2_06
> 2005/11/04 20:08:28:823 CET [DEBUG] HttpClient - Java vendor: Sun
> Microsystems Inc.
> 2005/11/04 20:08:28:829 CET [DEBUG] HttpClient - Operating system name:
> Linux
> 2005/11/04 20:08:28:831 CET [DEBUG] HttpClient - Operating system
> architecture: i386
> 2005/11/04 20:08:28:834 CET [DEBUG] HttpClient - Operating system version:
> 2.6.11.4-21.2-default
> 2005/11/04 20:08:29:375 CET [DEBUG] HttpClient - SUN 1.42: SUN (DSA
> key/parameter generation; DSA signing; SHA-1, MD5 digests; SecureRandom;
> X.509 certificates; JKS keystore; PKIX CertPathValidator; PKIX
> CertPathBuilder; LDAP, Collection CertStores)
> 2005/11/04 20:08:29:378 CET [DEBUG] HttpClient - SunJSSE 1.42: Sun JSSE
> provider(implements RSA Signatures, PKCS12, SunX509 key/trust factories,
> SSLv3, TLSv1)
> 2005/11/04 20:08:29:380 CET [DEBUG] HttpClient - SunRsaSign 1.42: SUN's
> provider for RSA signatures
> 2005/11/04 20:08:29:381 CET [DEBUG] HttpClient - SunJCE 1.42: SunJCE
> Provider (implements DES, Triple DES, AES, Blowfish, PBE, Diffie-Hellman,
> HMAC-MD5, HMAC-SHA1)
> 2005/11/04 20:08:29:381 CET [DEBUG] HttpClient - SunJGSS 1.0: Sun (Kerberos
> v5)
> 2005/11/04 20:08:29:418 CET [DEBUG] DefaultHttpParams - Set parameter
> http.useragent = Jakarta Commons-HttpClient/3.0-rc4
> 2005/11/04 20:08:29:428 CET [DEBUG] DefaultHttpParams - Set parameter
> http.protocol.version = HTTP/1.1
> 2005/11/04 20:08:29:445 CET [DEBUG] DefaultHttpParams - Set parameter
> http.connection-manager.class = class
> org.apache.commons.httpclient.SimpleHttpConnectionManager
> 2005/11/04 20:08:29:446 CET [DEBUG] DefaultHttpParams - Set parameter
> http.protocol.cookie-policy = rfc2109
> 2005/11/04 20:08:29:446 CET [DEBUG] DefaultHttpParams - Set parameter
> http.protocol.element-charset = US-ASCII
> 2005/11/04 20:08:29:447 CET [DEBUG] DefaultHttpParams - Set parameter
> http.protocol.content-charset = ISO-8859-1
> 2005/11/04 20:08:29:454 CET [DEBUG] DefaultHttpParams - Set parameter
> http.method.retry-handler =
> org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@4413ee
> 2005/11/04 20:08:29:456 CET [DEBUG] DefaultHttpParams - Set parameter
> http.dateparser.patterns = [EEE, dd MMM yyyy HH:mm:ss zzz, EEEE, dd-MMM-yy
> HH:mm:ss zzz, EEE MMM d HH:mm:ss yyyy, EEE, dd-MMM-yyyy HH:mm:ss z, EEE,
> dd-MMM-yyyy HH-mm-ss z, EEE, dd MMM yy HH:mm:ss z, EEE dd-MMM-yyyy HH:mm:ss
> z, EEE dd MMM yyyy HH:mm:ss z, EEE dd-MMM-yyyy HH-mm-ss z, EEE dd-MMM-yy
> HH:mm:ss z, EEE dd MMM yy HH:mm:ss z, EEE,dd-MMM-yy HH:mm:ss z,
> EEE,dd-MMM-yyyy HH:mm:ss z, EEE, dd-MM-yyyy HH:mm:ss z]
> 2005/11/04 20:08:29:819 CET [DEBUG] DefaultHttpParams - Set parameter
> http.protocol.cookie-policy = compatibility
> 2005/11/04 20:08:29:819 CET [DEBUG] DefaultHttpParams - Set parameter
> http.protocol.single-cookie-header = true
> 2005/11/04 20:08:29:907 CET [DEBUG] HttpConnection - Open connection to
> www.openbc.com:443
> 2005/11/04 20:08:30:822 CET [DEBUG] header - >> "POST
> /cgi-bin/user.fpl?op=home HTTP/1.1[\r][\n]"
> 2005/11/04 20:08:30:824 CET [DEBUG] HttpMethodBase - Adding Host request
> header
> 2005/11/04 20:08:30:867 CET [DEBUG] HttpMethodBase - Default charset used:
> ISO-8859-1
> 2005/11/04 20:08:30:885 CET [DEBUG] HttpMethodBase - Default charset used:
> ISO-8859-1
> 2005/11/04 20:08:30:896 CET [DEBUG] header - >> "User-Agent: Jakarta
> Commons-HttpClient/3.0-rc4[\r][\n]"
> 2005/11/04 20:08:30:897 CET [DEBUG] header - >> "Host:
> www.openbc.com[\r][\n]"
> 2005/11/04 20:08:30:898 CET [DEBUG] header - >> "Content-Length: 96[\r][\n]"
> 2005/11/04 20:08:30:899 CET [DEBUG] header - >> "Content-Type:
> application/x-www-form-urlencoded[\r][\n]"
> 2005/11/04 20:08:30:900 CET [DEBUG] header - >> "[\r][\n]"
> 2005/11/04 20:08:30:901 CET [DEBUG] content - >>
> "op=login&dest=%2Fcgi-bin%2Fuser.fpl%3Fop%3Dhome&login_user_name=***&login_password=***"
> 2005/11/04 20:08:31:668 CET [DEBUG] EntityEnclosingMethod - Request body
> sent
> 2005/11/04 20:08:32:165 CET [DEBUG] header - << "HTTP/1.1 302 Found[\r][\n]"
> 2005/11/04 20:08:32:182 CET [DEBUG] header - << "Date: Fri, 04 Nov 2005
> 19:07:47 GMT[\r][\n]"
> 2005/11/04 20:08:32:183 CET [DEBUG] header - << "Server: Apache[\r][\n]"
> 2005/11/04 20:08:32:183 CET [DEBUG] header - << "Set-Cookie: obc=;
> expires=Thu Jan  1 01:00:00 1970; path=/; domain=.www.openbc.com[\r][\n]"
> 2005/11/04 20:08:32:195 CET [DEBUG] header - << "Set-Cookie:
> obc_ssl=|UmFuZG9tSVZYaE9zNuvSFG6bZ8leX6RELVb4J3m3YE6rr+NOmguByop8Y1Ip+qTh2PjpRO8QLt8=|;
> expires=Sat Nov  4 20:07:47 2006; path=/[\r][\n]"
> 2005/11/04 20:08:32:197 CET [DEBUG] header - << "Set-Cookie:
> obc=|UmFuZG9tSVZYWvMi1Z/OXhtDWVqPapkrnwvDZcObDAkexpQuIlDjfjdji0du3jk7WZDuG32KJfM4XXAqV+gSQ27/i3R0zoWp5JC1zMeADEQ+7QyufISqDu9M8mz4pZTbmr1h4mjXT8w=|;
> secure; path=/[\r][\n]"
> 2005/11/04 20:08:32:199 CET [DEBUG] header - << "Cache-control:
> private[\r][\n]"
> 2005/11/04 20:08:32:199 CET [DEBUG] header - << "Expires: Now[\r][\n]"
> 2005/11/04 20:08:32:200 CET [DEBUG] header - << "Pragma: no-cache[\r][\n]"
> 2005/11/04 20:08:32:200 CET [DEBUG] header - << "Connection: close[\r][\n]"
> 2005/11/04 20:08:32:200 CET [DEBUG] header - << "Location:
> https://www.openbc.com/cgi-bin/user.fpl?op=home[\r][\n]"
> 2005/11/04 20:08:32:200 CET [DEBUG] header - << "Transfer-Encoding:
> chunked[\r][\n]"
> 2005/11/04 20:08:32:201 CET [DEBUG] header - << "Content-Type: text/html;
> charset=iso-8859-1[\r][\n]"
> 2005/11/04 20:08:32:351 CET [DEBUG] HttpMethodBase - Cookie accepted: "obc="
> 2005/11/04 20:08:32:353 CET [DEBUG] HttpMethodBase - Cookie accepted:
> "obc_ssl=|UmFuZG9tSVZYaE9zNuvSFG6bZ8leX6RELVb4J3m3YE6rr+NOmguByop8Y1Ip+qTh2PjpRO8QLt8=|"
> 2005/11/04 20:08:32:357 CET [DEBUG] HttpMethodBase - Cookie accepted:
> "obc=|UmFuZG9tSVZYWvMi1Z/OXhtDWVqPapkrnwvDZcObDAkexpQuIlDjfjdji0du3jk7WZDuG32KJfM4XXAqV+gSQ27/i3R0zoWp5JC1zMeADEQ+7QyufISqDu9M8mz4pZTbmr1h4mjXT8w=|"
> 2005/11/04 20:08:32:364 CET [DEBUG] HttpMethodDirector - Redirect required
> 2005/11/04 20:08:32:365 CET [INFO] HttpMethodDirector - Redirect requested
> but followRedirects is disabled
> 2005/11/04 20:08:32:366 CET [DEBUG] content - << "e"
> 2005/11/04 20:08:32:367 CET [DEBUG] content - << "3"
> 2005/11/04 20:08:32:367 CET [DEBUG] content - << " "
> 2005/11/04 20:08:32:367 CET [DEBUG] content - << "[\r]"
> 2005/11/04 20:08:32:367 CET [DEBUG] content - << "[\n]"
> 2005/11/04 20:08:32:373 CET [DEBUG] content - << "<!DOCTYPE HTML PUBLIC
> "-//IETF//DTD HTML 2.0//EN">[\n]"
> 2005/11/04 20:08:32:378 CET [DEBUG] content - << "<HTML><HEAD>[\n]"
> 2005/11/04 20:08:32:378 CET [DEBUG] content - << "<TITLE>302
> Found</TITLE>[\n]"
> 2005/11/04 20:08:32:378 CET [DEBUG] content - << "</HEAD><BODY>[\n]"
> 2005/11/04 20:08:32:379 CET [DEBUG] content - << "<H1>Found</H1>[\n]"
> 2005/11/04 20:08:32:380 CET [DEBUG] content - << "The document has moved <A
> HREF="https://www.openbc.com/cgi-bin/user.fpl?op=home">here</A>.<P>[\n]"
> 2005/11/04 20:08:32:380 CET [DEBUG] content - << "</BODY></HTML>[\n]"
> 2005/11/04 20:08:32:386 CET [DEBUG] content - << "[\r]"
> 2005/11/04 20:08:32:387 CET [DEBUG] content - << "[\n]"
> 2005/11/04 20:08:32:389 CET [DEBUG] content - << "0"
> 2005/11/04 20:08:32:389 CET [DEBUG] content - << "[\r]"
> 2005/11/04 20:08:32:389 CET [DEBUG] content - << "[\n]"
> 2005/11/04 20:08:32:390 CET [DEBUG] content - << "[\r]"
> 2005/11/04 20:08:32:390 CET [DEBUG] content - << "[\n]"
> 2005/11/04 20:08:32:390 CET [DEBUG] HttpMethodBase - Should close connection
> in response to directive: close
> 2005/11/04 20:08:32:392 CET [DEBUG] HttpConnection - Releasing connection
> back to connection manager.
> 2005/11/04 20:08:32:400 CET [DEBUG] HttpConnection - Open connection to
> www.openbc.com:443
> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
> <HTML><HEAD>
> <TITLE>302 Found</TITLE>
> </HEAD><BODY>
> <H1>Found</H1>
> The document has moved <A
> HREF="https://www.openbc.com/cgi-bin/user.fpl?op=home">here</A>.<P>
> </BODY></HTML>
> Present cookies: 
>  -
> obc_ssl=|UmFuZG9tSVZYaE9zNuvSFG6bZ8leX6RELVb4J3m3YE6rr+NOmguByop8Y1Ip+qTh2PjpRO8QLt8=|
>  -
> obc=|UmFuZG9tSVZYWvMi1Z/OXhtDWVqPapkrnwvDZcObDAkexpQuIlDjfjdji0du3jk7WZDuG32KJfM4XXAqV+gSQ27/i3R0zoWp5JC1zMeADEQ+7QyufISqDu9M8mz4pZTbmr1h4mjXT8w=|
> Redirect target: https://www.openbc.com/cgi-bin/user.fpl?op=home
> 2005/11/04 20:08:32:552 CET [DEBUG] header - >> "GET
> /cgi-bin/user.fpl?op=home HTTP/1.1[\r][\n]"
> 2005/11/04 20:08:32:553 CET [DEBUG] HttpMethodBase - Adding Host request
> header
> 2005/11/04 20:08:32:565 CET [DEBUG] HttpMethodDirector - Closing the
> connection.
> 2005/11/04 20:08:32:783 CET [DEBUG] HttpConnection - Releasing connection
> back to connection manager.
> Exception in thread "main" java.lang.ClassCastException
> 	at
> org.apache.commons.httpclient.params.DefaultHttpParams.getBooleanParameter(DefaultHttpParams.java:207)
> 	at
> org.apache.commons.httpclient.params.DefaultHttpParams.isParameterTrue(DefaultHttpParams.java:223)
> 	at
> org.apache.commons.httpclient.HttpMethodBase.addCookieRequestHeader(HttpMethodBase.java:1182)
> 	at
> org.apache.commons.httpclient.HttpMethodBase.addRequestHeaders(HttpMethodBase.java:1305)
> 	at
> org.apache.commons.httpclient.HttpMethodBase.writeRequestHeaders(HttpMethodBase.java:2036)
> 	at
> org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:1919)
> 	at
> org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:993)
> 	at
> org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:395)
> 	at
> org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
> 	at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
> 	at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
> 	at myClient.main(myClient.java:57)
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message