Himanshu,
HttpClient 2.0, unfortunately, cannot automatically handle cross-host redirects. But 
this limitation is not difficult to work around. See the document below

http://jakarta.apache.org/commons/httpclient/redirects.html

HttpClient 3.0 will address this limitation

Oleg

-----Original Message-----
From: Himanshu Pathak [mailto:[EMAIL PROTECTED]
Sent: Thursday, May 13, 2004 10:08
To: [EMAIL PROTECTED]
Subject: redirect issue with httpclient


  hi all,

I am using Jakarta HTTPClient to do a post on a website.
it seems that after the authentication the page is getting redirected to
other location.
the significant thing is that its changing the port also.

below i am posting the output i am getting from my code.

May 13, 2004 12:43:25 PM org.apache.commons.httpclient.HttpMethodBase
readResponseBody
WARNING: Response content length is not known
May 13, 2004 12:43:29 PM org.apache.commons.httpclient.HttpMethodBase
readResponseBody
WARNING: Response content length is not known
May 13, 2004 12:43:29 PM org.apache.commons.httpclient.HttpMethodBase
processRedirectResponse
INFO: Redirect requested but followRedirects is disabled
STATUS CODE = 302
May 13, 2004 12:43:32 PM org.apache.commons.httpclient.HttpMethodBase
readResponseBody
WARNING: Response content length is not known
May 13, 2004 12:43:32 PM org.apache.commons.httpclient.HttpMethodBase
processRedirectResponse
WARNING: Redirect from port 2048 to 2096 is not supported*
*

how can i make httpclient to get redirected to the new location
automatically.


here is my code:


import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class FormLoginDemo
{
    static final String LOGON_SITE = "www.somedomain.com";
    static final int    LOGON_PORT = 2048;
    static final String PROXY_HOST = "192.168.10.1";
    static final int PROXY_PORT = 80;
      
    public static void main(String[] args) throws Exception
    {
        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT,
"http");
      
        client.getHostConfiguration().setProxy(PROXY_HOST,PROXY_PORT);
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
      
        GetMethod authget = new GetMethod("/login");
   
        client.executeMethod(authget);
        Header location = authget.getResponseHeader("Location");
        authget.releaseConnection();
      
        PostMethod authpost    = new PostMethod("/login");
        authpost.setFollowRedirects(true);
        NameValuePair action   = new NameValuePair("action", "login");
        NameValuePair url      = new NameValuePair("url", "someurl");
        NameValuePair userid   = new NameValuePair("user", "myuser");
        NameValuePair password = new NameValuePair("pass", "mypassword");
        authpost.setRequestBody( new NameValuePair[] {action, url,
userid, password});
      
        client.executeMethod(authpost);
        authpost.releaseConnection();

        int statuscode = authpost.getStatusCode();
      
        System.out.println("STATUS CODE = "+statuscode);
      
        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 = "/";
                GetMethod redirect = new GetMethod(newuri);
                client.executeMethod(redirect);
                redirect.releaseConnection();
            }
            else
            {
                System.out.println("Invalid redirect");
                System.exit(1);
            }
        }
    }
}

any help in this context will be highly appreciated.


Regards.

Himanshu.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


***************************************************************************************************
The information in this email is confidential and may be legally privileged.  Access 
to this email by anyone other than the intended addressee is unauthorized.  If you are 
not the intended recipient of this message, any review, disclosure, copying, 
distribution, retention, or any action taken or omitted to be taken in reliance on it 
is prohibited and may be unlawful.  If you are not the intended recipient, please 
reply to or forward a copy of this message to the sender and delete the message, any 
attachments, and any copies thereof from your system.
***************************************************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to