You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Himanshu Pathak <hp...@tecnovaglobal.com> on 2004/05/13 10:08:28 UTC

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: commons-httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org