Hello, Thanks a lot for your explanation Oleg and François-Xavier.
Best Regards. From: Francois-Xavier Bonnet [mailto:[email protected]] On Behalf Of François-Xavier Bonnet Sent: mercredi 6 février 2013 14:30 To: COURTAULT Francois Cc: HttpClient User Discussion; [email protected]; [email protected] Subject: Re: POST redirection question Francois, What I meant is that changing POST to GET for a 301 redirect is contrary to the specification (and you are right) but this behavior is now a de facto standard. Most servers expect this behavior so following the specification on this point would cause a lot of problems. There is an interesting article about it here: http://www.alanflavell.org.uk/www/post-redirect.html As Oleg already mentioned, you may change this easily by writing a subclass of org.apache.http.impl.client.DefaultRedirectStrategy you will have to rewrite the method that builds the new request when the redirect occurs: org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(HttpRequest, HttpResponse, HttpContext) On 06/02/2013 09:43, COURTAULT Francois wrote: Hello François-Xavier, I was not talking of browser behavior. I was talking of HttpClient behavior (eg HttpClient user agent). This is why I have asked the spec question. Best Regards. -----Original Message----- From: Francois-Xavier Bonnet [mailto:[email protected]] Sent: mercredi 6 février 2013 09:31 To: HttpClient User Discussion Cc: [email protected]<mailto:[email protected]> Subject: RE: POST redirection question Hello Francois, Browsers do not respect much this part of the specifications. I remember I made some comprehensive tests (but this was a few years ago) and most browsers were changing POST to GET. Le 6 févr. 2013 09:15, "COURTAULT Francois" <[email protected]><mailto:[email protected]> a écrit : Hello Oleg, First, thanks a lot for you answer. Is it written in the rfc 2626 spec because I have not seen it :-( I have only seen at 10.3.2: If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued. Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request. So my understanding is that the default behavior for HTTP/1.1 user agent is to not change the HTTP request (eg keep the POST request). Am I wrong ? But maybe the first paragraph prevails meaning that for 301 user agent automatic redirection is not allowed: right ? Best Regards. -----Original Message----- From: Oleg Kalnichevski [mailto:[email protected]] Sent: mardi 5 février 2013 23:31 To: HttpClient User Discussion Subject: Re: POST redirection question On Tue, 2013-02-05 at 18:53 +0100, COURTAULT Francois wrote: Hello everyone, At the beginning, I had something like that : HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(SOME_URL); HttpResponse postResponse = httpClient.execute(httpPost); It turns out that I got a 301 Moved permanently. So I made the following modifications, either: - httpClient.setRedirectStrategy(new LaxRedirectStrategy()); - or httpClient.setRedirectStrategy(new DefaultRedirectStrategy()); - or httpClient.setRedirectStrategy(new DefaultRedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) { boolean isRedirected = false; try { isRedirected = super.isRedirected(request, response, context); } catch (ProtocolException e) { fail("Unable to set a redirect strategy, reason: " + e.getMessage()); } if (!isRedirected) { int responseCode = response.getStatusLine().getStatusCode(); if (responseCode == 301 || responseCode == 302) { return true; } } return false; } }); - or the same than above with new LaxRedirectStrategy Each time, after receiving a 301, the client sent a GET request instead of my initial HTTP request, which is a POST one as you can see, to the new location. Any advice ? sample ? Or is it an issue ? Best Regards. Only in case of a TEMPORARY_REDIRECT it is valid to redirect the request without changing its method. In all other cases methods other than HEAD and GET get converted to GET. To change this behavior you need to override #getRedirect method http://hc.apache.org/httpcomponents-client-ga/httpclient/xref/org/apac he/http/impl/client/DefaultRedirectStrategy.html#213 Oleg -------------------------------------------------------------------- - To unsubscribe, e-mail: [email protected]<mailto:[email protected]> For additional commands, e-mail: [email protected]<mailto:[email protected]> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected]<mailto:[email protected]> For additional commands, e-mail: [email protected]<mailto:[email protected]> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected]<mailto:[email protected]> For additional commands, e-mail: [email protected]<mailto:[email protected]>
