Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Httpclient 4, error 302. How to redirect?

Tags:

I want to access one site that first requires an (tomcat server) authentication and then log in with a POST request and keep that user to see the site's pages. I use Httpclient 4.0.1

The first authentication works fine but not the logon that always complains about this error: "302 Moved Temporarily"

I keep cookies & I keep a context and yet nothing. Actually, it seems that the logon works, because if I write incorrect parameters or user||password, I see the login page. So I guess what doesn't work is the automatic redirection.

Following my code, which always throws the IOException, 302:

    DefaultHttpClient httpclient = new DefaultHttpClient();     CookieStore cookieStore = new BasicCookieStore();     httpclient.getParams().setParameter(       ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);      HttpContext context = new BasicHttpContext();     context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);     //ResponseHandler<String> responseHandler = new BasicResponseHandler();      Credentials testsystemCreds = new UsernamePasswordCredentials(TESTSYSTEM_USER,  TESTSYSTEM_PASS);     httpclient.getCredentialsProvider().setCredentials(             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),             testsystemCreds);      HttpPost postRequest = new HttpPost(cms + "/login");     List<NameValuePair> formparams = new ArrayList<NameValuePair>();     formparams.add(new BasicNameValuePair("pUserId", user));     formparams.add(new BasicNameValuePair("pPassword", pass));     postRequest.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));     HttpResponse response = httpclient.execute(postRequest, context);     System.out.println(response);      if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)         throw new IOException(response.getStatusLine().toString());      HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(              ExecutionContext.HTTP_REQUEST);     HttpHost currentHost = (HttpHost)  context.getAttribute(              ExecutionContext.HTTP_TARGET_HOST);     String currentUrl = currentHost.toURI() + currentReq.getURI();             System.out.println(currentUrl);      HttpEntity entity = response.getEntity();     if (entity != null) {         long len = entity.getContentLength();         if (len != -1 && len < 2048) {             System.out.println(EntityUtils.toString(entity));         } else {             // Stream content out         }     } 
like image 241
juanmirocks Avatar asked Sep 07 '10 12:09

juanmirocks


People also ask

How do you resolve a 302 redirect?

You can follow these five steps to fix HTTP 302 errors on your website: Determine whether the redirects are appropriate or not by examining the URLs that are issuing the 302 redirects. Check your plugins to make sure any redirect settings are valid. Ensure that your WordPress URL settings are configured correctly.

Does a 302 automatically redirect?

If the 302 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.

What is a 302 redirect error?

The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.

How do I follow a redirect in Java?

Java Http Redirect ExampleIf a server is redirected from the original URL to another URL, the response code should be 301: Moved Permanently or 302: Temporary Redirect. And you can get the new redirected url by reading the “Location” header of the HTTP response header.


1 Answers

For 4.1 version:

DefaultHttpClient  httpclient = new DefaultHttpClient();     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) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }             if (!isRedirect) {                 int responseCode = response.getStatusLine().getStatusCode();                 if (responseCode == 301 || responseCode == 302) {                     return true;                 }             }             return isRedirect;         }     }); 
like image 119
fvisticot Avatar answered Sep 24 '22 03:09

fvisticot