Httpclient redirect for newbies [closed]?

Here's a sample adapted from the 'Response Handling' example here. Your example is quite complicated - best to simplify your code while you figure out how to follow redirects (you can comment out the section I've highlighted to show the example failing to follow the redirect). Import org.apache.http.

*; import org.apache.http.client. *; import org.apache.http.client.methods. *; import org.apache.http.impl.client.

*; import org.apache.http.protocol. *; public class ClientWithResponseHandler { public final static void main(String args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); // Comment out from here (Using /* and */)... httpclient. SetRedirectStrategy(new DefaultRedirectStrategy() { public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) { boolean isRedirect=false; try { isRedirect = super.

IsRedirected(request, response, context); } catch (ProtocolException e) { e.printStackTrace(); } if (!isRedirect) { int responseCode = response.getStatusLine().getStatusCode(); if (responseCode == 301 || responseCode == 302) { return true; } } return false; } }); // ...to here and the request will fail with "HttpResponseException: Moved Permanently" try { HttpPost httpPost = new HttpPost("http://news.bbc.co.uk/"); System.out. Println("executing request " + httpPost.getURI()); // Create a response handler ResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = httpclient. Execute(httpPost, responseHandler); System.out.

Println(responseBody); // Add your code here... } finally { // When HttpClient instance is no longer needed, shut down the connection // manager to ensure immediate deallocation of all system resources httpclient. GetConnectionManager().shutdown(); } } }.

Thanks. I will take a look at this soon. – Brian Sep 27 at 14:21 I'm editing my response to reflect my new code based on your suggestions.

I will show what is between try and finally in your proposed answer. The first responseBody returns as expected. I am not sure I am performing the second request correct to allow redirects.

The first URL which must be submitted to login receives a 200. It is the second request that gets a 302. According to firebug, a cima.

Ticket post must be submitted, so I captured that from responseBody and put it into the post array. I am getting an error HttpResponse exception found. – Brian Sep 28 at 0:35 I think what one problem may be is that you can only be redirected to the form, but my guess is that you are only submitting form data for the site that redirects you.Is there a way to get it to submit form data for ever redirect?

– Brian Sep 29 at 0:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions