Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpclient 4, error 302. How to redirect?
    primarykey
    data
    text
    <p>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</p> <p>The first authentication works fine but not the logon that always complains about this error: "302 Moved Temporarily"</p> <p>I keep cookies &amp; 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. </p> <p>Following my code, which always throws the IOException, 302:</p> <pre><code> 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&lt;String&gt; 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&lt;NameValuePair&gt; formparams = new ArrayList&lt;NameValuePair&gt;(); 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 &amp;&amp; len &lt; 2048) { System.out.println(EntityUtils.toString(entity)); } else { // Stream content out } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload