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 Andreas Blomqvist <bl...@gmail.com> on 2010/02/12 08:54:47 UTC

new to the list

Hi all

New to this list and I am not sure if my mails are getting thru. Anyway,
got problem with posting a form and loggin in. The post goes thru and I get
a 302 back. The HttpClient should handle the redirect by default right? The
problem is that I am not getting logged in on the site until i make a new
call with a new HttpClient. Everyhing is in a servlet, and first call to the
servlet doesnt logg me in, but the next one does. My post form code:

As you can see i'm trying to manually redirect also but i doesnt make any
difference.

   HttpResponse response = null;
        HttpEntity entity = null;
        HttpPost httpost = new HttpPost(BASE_URL
+"/index.php?func=do_login");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("btn_login", button));
        nvps.add(new BasicNameValuePair("txt_login", user));
        nvps.add(new BasicNameValuePair("psw_password", pass));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        HttpContext context = new BasicHttpContext();
        response = httpClient.execute(httpost, context);
        entity = response.getEntity();
        if (entity != null) {
            entity.consumeContent();
        }
        HttpUriRequest request = (HttpUriRequest) context.getAttribute(
                ExecutionContext.HTTP_REQUEST);

        String uri = BASE_URL + request.getURI().toString();

        HttpGet redirect = new HttpGet(uri);
        httpClient.execute(redirect, context);

        return context;

thanks

Re: new to the list

Posted by Ken Krugler <kk...@transpac.com>.
Hi Andreas,

See below, though I haven't used HttpClient 4 for form posting, so I  
could be way off base.

On Feb 11, 2010, at 11:54pm, Andreas Blomqvist wrote:

> Hi all
>
> New to this list and I am not sure if my mails are getting thru.  
> Anyway,
> got problem with posting a form and loggin in. The post goes thru  
> and I get
> a 302 back. The HttpClient should handle the redirect by default  
> right?

The redirect on a POST isn't the same as a redirect on a GET. In the  
case of the GET, the redirect can be automatically handled since (a) a  
GET should have no side effects, and (b) you're using the same  
GetMethod to make another request.

A POST that returns a redirect needs to be followed by a GET with the  
new location.

> The
> problem is that I am not getting logged in on the site until i make  
> a new
> call with a new HttpClient. Everyhing is in a servlet, and first  
> call to the
> servlet doesnt logg me in, but the next one does.

You shouldn't need a new HttpClient, but you should set up & reuse a  
local context so that any cookies returned by the POST are available  
for the GET.

-- Ken

> My post form code:
>
> As you can see i'm trying to manually redirect also but i doesnt  
> make any
> difference.
>
>   HttpResponse response = null;
>        HttpEntity entity = null;
>        HttpPost httpost = new HttpPost(BASE_URL
> +"/index.php?func=do_login");
>
>        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
>        nvps.add(new BasicNameValuePair("btn_login", button));
>        nvps.add(new BasicNameValuePair("txt_login", user));
>        nvps.add(new BasicNameValuePair("psw_password", pass));
>
>        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
>
>        HttpContext context = new BasicHttpContext();
>        response = httpClient.execute(httpost, context);
>        entity = response.getEntity();
>        if (entity != null) {
>            entity.consumeContent();
>        }
>        HttpUriRequest request = (HttpUriRequest) context.getAttribute(
>                ExecutionContext.HTTP_REQUEST);
>
>        String uri = BASE_URL + request.getURI().toString();
>
>        HttpGet redirect = new HttpGet(uri);
>        httpClient.execute(redirect, context);
>
>        return context;
>
> thanks

--------------------------------------------
Ken Krugler
+1 530-210-6378
http://bixolabs.com
e l a s t i c   w e b   m i n i n g





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