You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by "Rodrigues, Reeve - PA (Consultant)" <Re...@gmacm.com> on 2007/02/02 17:35:45 UTC

question about following redirects

Hi I am a newbie and was wondering if some gracious soul could help out.


 

What I am trying to do, is post some data to a form, if the form sees
the key "secure_payload' in the query string it needs to perform an auto
login into the site vs a traditional login. It then redirects me to a
new page which I need to get the response and display 

Rather when I  try to follow the redirect url by doing a  get 

I get a 

   Method failed : HTTP/1.1 320 Found , when I do a post . Which is what
I expect as I need to follow this to the new resource.

   <html><head><title>Object moved</title></head><body> 

   <h2>Object moved to <a href="/AWD/login.aspx">here</a>.</h2>

  </body></html>

 

Then it throws 

java.lang.IllegalArgumentException: host parameter is null 

 

I get no host ? I am confused? do I need to set some thing how does the
new url get know the host information ?

 

Appreciate your help

Thank you

Reeve

 

 

 

 

 

########################################################################
################################################################

This is what I have in my code

########################################################################
###############################################################

package com.gmacb.blt.whlstruts.assetwisedirect.utility;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.commons.httpclient.*;

 

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.log4j.Logger;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

 

public class CopyOfAwdSSOPost3 {

     final private static Logger log =
Logger.getLogger(CopyOfAwdSSOPost3.class);

 

      public CopyOfAwdSSOPost3(){}

      public void sendToAwdServer(String url,String params) throws
IOException{

   

          String response= null;

 

 

            HttpClient client = new HttpClient();

       

      PostMethod post = new PostMethod(url);

      post.setFollowRedirects(true); 

        post.addParameter("secure_payload",params);

      

        try {

            // execute the post

            int statusCode = client.executeMethod( post );

 

            //check the response code

            // 200 status ok

            if (statusCode != HttpStatus.SC_OK) {

                System.out.println("Method failed: " +
post.getStatusLine());

            }    

 

            // Read the response body.

            InputStreamReader is = new
InputStreamReader(post.getResponseBodyAsStream() );

          BufferedReader in = new BufferedReader( is );

                  // Use caution: ensure correct character encoding and
is not binary data

                  for ( String s; ( s = in.readLine() ) != null; )

                        System.out.println( s );

          

                  

                System.out.println("status:" + post.getStatusLine());

 

                //close connection

                post.releaseConnection();

                  

                  

                  // Tell me about the redirect.

                int statuscode = post.getStatusCode();

              if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||

                  (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||

                  (statuscode == HttpStatus.SC_SEE_OTHER) ||

                  (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

                  Header header = post.getResponseHeader("location");

                  if (header != null) {

                      String newuri = header.getValue();

                      if ((newuri == null) || (newuri.equals(""))) {

                          newuri = "/";

                      }

                      //System.out.println("Redirect target: " +
newuri); 

                      GetMethod redirect = new GetMethod(newuri);

 

                      //Execute the method.

                      statuscode =client.executeMethod(redirect);

                      if (statusCode != HttpStatus.SC_OK) {

                                    System.err.println("Method failed:
"+ redirect.getStatusLine());

                              }

 

                              // Read the response body.

                              is = new
InputStreamReader(redirect.getResponseBodyAsStream() );

                              in = new BufferedReader( is );

 

                              // Deal with the response.

                              // Use caution: ensure correct character
encoding and is not binary data

                              for ( String s; ( s = in.readLine() ) !=
null;)

                                   System.out.println( s );

                      

                      // release any connection resources used by the
method

                      redirect.releaseConnection();

                  } else {

                      //System.out.println("Invalid redirect");

                      System.exit(1);

                  }

              }  

                  

                  

 

 

 

        } catch (HttpException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

 

 

          

          return response;

        

      }

    

 

      public static void main(String[] args) throws IOException {

/*   

        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.header", "debug");

 

 
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.
commons.httpclient", "debug");

*/         

        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");

 

       

        

          

              

          String
url="https://secureshrtyukon.rfc.com/awd/AutoLogon/login.aspx";

        

        System.out.println("url: "+url);

          String msg = new
AssetWisePararmeterBuilder().buildParamters("reeve");

          System.out.println("msg: "+msg);

          ReadSSOXMLFile sSSOXmlFile = new
ReadSSOXMLFile("C:\\generic_sso_sample_app_password\\GenericTestHarness\
\generic_sso_publickey.xml");

          String encodedMsg = new
RSAEncryptor().getBase64EncodedMessage(sSSOXmlFile.getbase64DecodedModul
us(), sSSOXmlFile.getbase64DecodedExponent(), msg);

 

         System.out.println("encoded msg: "+encodedMsg);

         CopyOfAwdSSOPost3 send = new CopyOfAwdSSOPost3();

         send.sendToAwdServer(url,encodedMsg);

         

      }

  }

 

########################################################################
######################################

This is the wire trace

########################################################################
###############################

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -Java version: 1.4.2_13

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -Java vendor: Sun
Microsystems Inc.

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -Java class path:
2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -Operating system name:
Windows XP

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -Operating system
architecture: x86

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -Operating system
version: 5.1

2007/02/02 11:25:59:150 EST [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)

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -SunJSSE 1.42: Sun JSSE
provider(implements RSA Signatures, PKCS12, SunX509 key/trust factories,
SSLv3, TLSv1)

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -SunRsaSign 1.42: SUN's
provider for RSA signatures

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -SunJCE 1.42: SunJCE
Provider (implements DES, Triple DES, AES, Blowfish, PBE,
Diffie-Hellman, HMAC-MD5, HMAC-SHA1)

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -SunJGSS 1.0: Sun
(Kerberos v5)

2007/02/02 11:25:59:150 EST [DEBUG] HttpClient - -BC 1.35: BouncyCastle
Security Provider v1.35

2007/02/02 11:25:59:230 EST [DEBUG] HttpConnection -
-HttpConnection.setSoTimeout(0)

2007/02/02 11:25:59:401 EST [DEBUG] HttpMethodBase - -Execute loop try 1

2007/02/02 11:25:59:401 EST [DEBUG] wire - ->> "POST
/awd/AutoLogon/login.aspx HTTP/1.1[\r][\n]"

2007/02/02 11:25:59:411 EST [DEBUG] HttpMethodBase - -Adding Host
request header

2007/02/02 11:25:59:421 EST [DEBUG] HttpMethodBase - -Default charset
used: ISO-8859-1

2007/02/02 11:25:59:421 EST [DEBUG] wire - ->> "User-Agent: Jakarta
Commons-HttpClient/2.0rc2[\r][\n]"

2007/02/02 11:25:59:421 EST [DEBUG] wire - ->> "Host:
xyz.rfc.com[\r][\n]"

2007/02/02 11:25:59:421 EST [DEBUG] wire - ->> "Content-Length:
197[\r][\n]"

2007/02/02 11:25:59:421 EST [DEBUG] wire - ->> "Content-Type:
application/x-www-form-urlencoded[\r][\n]"

2007/02/02 11:25:59:651 EST [DEBUG] wire - ->> "[\r][\n]"

2007/02/02 11:25:59:651 EST [DEBUG] EntityEnclosingMethod - -Using
buffered request body

2007/02/02 11:25:59:661 EST [DEBUG] wire - ->>
"secure_payload=yfz4xvQDifUpE8ELN1Y4FItL1lSXDF%2F2pYvGc623gHGBwfYECwhjAD
5PYzTpsojlnDJjYf8%2BPuw5Zp7eZxV2J%2FOFzXjLKgHdEsk6P4ZUDhLhz9UIG3veYBx36j
p%2FMdppNrPq2OkUq9WDyJUxyzUHSonr7F5hvD0F8ajGIeOAzw0%3D"

2007/02/02 11:25:59:661 EST [DEBUG] EntityEnclosingMethod - -Request
body sent

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "HTTP/1.1 100
Continue[\r][\n]"

2007/02/02 11:25:59:791 EST [INFO] HttpMethodBase - -Discarding
unexpected response: HTTP/1.1 100 Continue

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "HTTP/1.1 302
Found[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Set-Cookie:
secureshrtyukon_CKE=1849206976.20480.0000; path=/[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Date: Fri, 02 Feb 2007
16:25:55 GMT[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Server:
Microsoft-IIS/6.0[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "X-Powered-By:
ASP.NET[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "X-AspNet-Version:
2.0.50727[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Location:
/AWD/login.aspx[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Set-Cookie:
ASP.NET_SessionId=asq3eb55zsxpfwjwrzszgp45; path=/; HttpOnly[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Cache-Control:
private[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Content-Type: text/html;
charset=utf-8[\r][\n]"

2007/02/02 11:25:59:791 EST [DEBUG] wire - -<< "Content-Length:
132[\r][\n]"

2007/02/02 11:25:59:822 EST [DEBUG] HttpMethodBase - -Cookie accepted:
"$Version=0; secureshrtyukon_CKE=1849206976.20480.0000; $Path=/"

2007/02/02 11:25:59:832 EST [DEBUG] CookieSpec - -Unrecognized cookie
attribute: name=HttpOnly, value=null

2007/02/02 11:25:59:832 EST [DEBUG] HttpMethodBase - -Cookie accepted:
"$Version=0; ASP.NET_SessionId=asq3eb55zsxpfwjwrzszgp45; $Path=/"

2007/02/02 11:25:59:832 EST [DEBUG] HttpMethodBase - -Redirect required

2007/02/02 11:25:59:832 EST [INFO] HttpMethodBase - -Redirect requested
but followRedirects is disabled

Method failed: HTTP/1.1 302 Found

2007/02/02 11:25:59:832 EST [DEBUG] wire - -<<
"<html><head><title>Object moved</title></head><body>[\r][\n]"

2007/02/02 11:25:59:832 EST [DEBUG] wire - -<< "<h2>Object moved to <a
href="/AWD/login.aspx">here</a>.</h2>[\r][\n]"

2007/02/02 11:25:59:832 EST [DEBUG] wire - -<< "</body></html>[\r][\n]"

<html><head><title>Object moved</title></head><body>

<h2>Object moved to <a href="/AWD/login.aspx">here</a>.</h2>

</body></html>

2007/02/02 11:25:59:832 EST [DEBUG] HttpMethodBase - -Resorting to
protocol version default close connection policy

2007/02/02 11:25:59:832 EST [DEBUG] HttpMethodBase - -Should NOT close
connection, using HTTP/1.1.

status:HTTP/1.1 302 Found

java.lang.IllegalArgumentException: host parameter is null

      at
org.apache.commons.httpclient.HttpConnection.setHost(HttpConnection.java
:277)

      at
org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnection(
SimpleHttpConnectionManager.java:149)

      at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:6
45)

      at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:5
29)

      at
com.gmacb.blt.whlstruts.assetwisedirect.utility.CopyOfAwdSSOPost3.sendTo
AwdServer(CopyOfAwdSSOPost3.java:78)

      at
com.gmacb.blt.whlstruts.assetwisedirect.utility.CopyOfAwdSSOPost3.main(C
opyOfAwdSSOPost3.java:152)

Exception in thread "main"

 


Re: question about following redirects

Posted by Roland Weber <ht...@dubioso.net>.
> Ahh!! This is the culprit the relative url, Thank you. I have never
> attempted resolving url? Do I take the host info and append the relative
> url ? Should that do the trick? 

http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/URI.html#URI(org.apache.commons.httpclient.URI,%20java.lang.String,%20boolean)
http://java.sun.com/j2se/1.4.2/docs/api/java/net/URI.html#resolve(java.lang.String)
http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html#URL(java.net.URL,%20java.lang.String)

> But what the session id?

It's either a cookie or in the /path?query part.

cheers,
  Roland


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


RE: question about following redirects

Posted by "Rodrigues, Reeve - PA (Consultant)" <Re...@gmacm.com>.
Hi Roland
  Sorry for the confusion 

>Have you enabled or disabled automatic redirects? In other words, do
>you have a problem with the default behavior of HttpClient, or one in
>your application?

I have enabled redirects, it works fine from the post. 

>Do you get that problem when executing the POST, or when doing
the followup GET? 

No problems from the post, the post return the status code of 302, which
is the redirect I need to follow

>Is the "Method failed" a message from your code?
It's the message from the code, it's the status line from the post
message , as usually u look for 200 for success . I did not update that
to infer redirection 

>320 is not a defined status code. Message "Found" is assigned to status
>code 302.

Oops!! Sorry for soo many newbie mistakes , I do apologise once again as
it made it difficult for you to help me.


>Servers are required to send absolute URLs in a redirect, but
>many don't. If you've tried to send a GET request to a relative
>URL, this is the what you might see. You have to resolve the
>relative URL manually.

Ahh!! This is the culprit the relative url, Thank you. I have never
attempted resolving url? Do I take the host info and append the relative
url ? Should that do the trick? But what the session id? 

Thank for your help
Cheers
-reeve

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


Re: question about following redirects

Posted by Roland Weber <ht...@dubioso.net>.
Hello Reeve,

> What I am trying to do, is post some data to a form, if the form sees
> the key "secure_payload' in the query string it needs to perform an auto
> login into the site vs a traditional login. It then redirects me to a
> new page which I need to get the response and display 

Have you enabled or disabled automatic redirects? In other words, do
you have a problem with the default behavior of HttpClient, or one in
your application?

> Rather when I  try to follow the redirect url by doing a  get 
> 
> I get a 
> 
>    Method failed : HTTP/1.1 320 Found , when I do a post .

Do you get that problem when executing the POST, or when doing
the followup GET? Is the "Method failed" a message from your code?
320 is not a defined status code. Message "Found" is assigned to
status code 302.

> Which is what
> I expect as I need to follow this to the new resource.
> 
>    <html><head><title>Object moved</title></head><body> 
> 
>    <h2>Object moved to <a href="/AWD/login.aspx">here</a>.</h2>
> 
>   </body></html>
> 
>  
> 
> Then it throws 
> 
> java.lang.IllegalArgumentException: host parameter is null 

Servers are required to send absolute URLs in a redirect, but
many don't. If you've tried to send a GET request to a relative
URL, this is the what you might see. You have to resolve the
relative URL manually.

> ########################################################################
> This is what I have in my code

>             //check the response code
> 
>             // 200 status ok
> 
>             if (statusCode != HttpStatus.SC_OK) {
> 
>                 System.out.println("Method failed: " +
> post.getStatusLine());
> 
>             }    

If you expect a redirect with status code 302, why do you
print "Method failed" if you get anything but status 200?

I didn't look into the log, see Oleg's comment on HttpClient version.

cheers,
  Roland

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


Re: question about following redirects

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2007-02-02 at 10:35 -0600, Rodrigues, Reeve - PA (Consultant)
wrote:
> Hi I am a newbie and was wondering if some gracious soul could help out.
> 
> 
>  
> 
> What I am trying to do, is post some data to a form, if the form sees
> the key "secure_payload' in the query string it needs to perform an auto
> login into the site vs a traditional login. It then redirects me to a
> new page which I need to get the response and display 
> 
> Rather when I  try to follow the redirect url by doing a  get 
> 
> I get a 
> 
>    Method failed : HTTP/1.1 320 Found , when I do a post . Which is what
> I expect as I need to follow this to the new resource.
> 
>    <html><head><title>Object moved</title></head><body> 
> 
>    <h2>Object moved to <a href="/AWD/login.aspx">here</a>.</h2>
> 
>   </body></html>
> 
>  
> 
> Then it throws 
> 
> java.lang.IllegalArgumentException: host parameter is null 
> 
>  
> 
> I get no host ? I am confused? do I need to set some thing how does the
> new url get know the host information ?
> 
>  
> 
> Appreciate your help
> 
> Thank you
> 
> Reeve
> 



> Jakarta Commons-HttpClient/2.0rc2

Reeve,

I wish I could do a bit more for you but we no longer support HttpClient
2.0 let alone its RC2 which had numerous bug fixes. Please consider
upgrading to HttpClient 3.0.1 or at the very least to HttpClient 2.0.2
(the last stable release in the discontinued 2.0.x branch).

Oleg


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