Return-Path: X-Original-To: apmail-hc-httpclient-users-archive@www.apache.org Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DDF42DADF for ; Mon, 17 Sep 2012 20:11:12 +0000 (UTC) Received: (qmail 45401 invoked by uid 500); 17 Sep 2012 20:11:12 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 45366 invoked by uid 500); 17 Sep 2012 20:11:12 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 45346 invoked by uid 99); 17 Sep 2012 20:11:12 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Sep 2012 20:11:12 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [217.150.250.48] (HELO kalnich.nine.ch) (217.150.250.48) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Sep 2012 20:10:33 +0000 Received: from [192.168.1.147] (77-57-197-206.dclient.hispeed.ch [77.57.197.206]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by kalnich.nine.ch (Postfix) with ESMTPSA id A2C1FB801EB for ; Mon, 17 Sep 2012 22:10:11 +0200 (CEST) Message-ID: <1347912610.2493.14.camel@ubuntu> Subject: Re: 4.2.1 - 302 Redirect From: Oleg Kalnichevski To: HttpClient User Discussion Date: Mon, 17 Sep 2012 22:10:10 +0200 In-Reply-To: <5057280C.7020603@gmx.de> References: <5057280C.7020603@gmx.de> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.2.3-0ubuntu6 Content-Transfer-Encoding: 7bit Mime-Version: 1.0 X-Virus-Checked: Checked by ClamAV on apache.org On Mon, 2012-09-17 at 15:39 +0200, Gerd Schmidt wrote: > Hi, > > I want to get the body from a page, which is redirected from a https adress. > > I'm using Version 4.2.1 and Eclipse Java EE IDE for Web Developers > Version: Indigo Service Release 2 > > I read the tutorials and this is my code: > > public final static void main(String[] args) throws Exception { > > HttpClient httpclient = new DefaultHttpClient(); > > try { > > HttpPost httpost = new HttpPost("https://xxx.xx"); > > List nvps = new ArrayList (); > nvps.add(new BasicNameValuePair("login", "abc")); > nvps.add(new BasicNameValuePair("password", "abc")); > > httpost.setEntity(new UrlEncodedFormEntity(nvps, > Consts.UTF_8)); > > HttpResponse response = httpclient.execute(httpost); > > HttpEntity entity = response.getEntity(); > System.out.println("Login form get: " + > response.getStatusLine()); > EntityUtils.consume(entity); > > } finally { > httpclient.getConnectionManager().shutdown(); > } > } > > It works, I get a "HTTP/1.1 302 Moved Temporarily", so I checked the > HEADER for the redirectLocation. > > String redirectLocation = null; > Header locationHeader = response.getFirstHeader("location"); > if (locationHeader != null) { > redirectLocation = locationHeader.getValue(); > } > > This works too, I got the new adress. My Problem is, I want to get the > body of the redirected location (while I'm logged in). > So i tried: > > HttpGet httpget = new HttpGet(redirectLocation); > ResponseHandler responseHandler = new > BasicResponseHandler(); > String responseBody = httpclient.execute(httpget, > responseHandler); > System.out.println(responseBody); > > But this is not the page, with the updated content i wanted. Furthermore > I read, I have to set a "RedirectStrategy", so i tried > putting > > ((AbstractHttpClient) httpclient).setRedirectStrategy(new > LaxRedirectStrategy()); > > after HttpClient httpclient = new DefaultHttpClient(); still no change. > (Anyway, why is httpclient.setRedirectStrategy(new > LaxRedirectStrategy()); not working?) > > So, my Question is, "How do i handle a 302 redirect an get the > redirected body". > > Thank you > Gerd Schmidt > Gerd, You basically have two options: either implement a custom RedirectStrategy (LaxRedirectStrategy is not going to help here as it is intended for a completely different case) or disable automatic redirects and handle all redirect status codes manually. Oleg --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org For additional commands, e-mail: httpclient-users-help@hc.apache.org