Hello guys,
I'm developing an app to do a POST log in on a server and access another page (who need logged status) to use those information.
The code:
public static void main(String[] args) throws IllegalStateException, IOException { SSLContext ctx = null; try { ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } SSLSocketFactory sf = new SSLSocketFactory(ctx); Scheme httpsScheme = new Scheme("https", 443, sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry); DefaultHttpClient client = new DefaultHttpClient(cm); client.setCookieStore(cookieStore); //POST method HttpPost post = new HttpPost("https://localhost/loginServlet"); //Post Parameters List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("Username", "usertest")); nameValuePairs.add(new BasicNameValuePair("Password", "passtest")); nameValuePairs.add(new BasicNameValuePair("cmdConfirmar", "Confirmar")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8)); //Do POST HttpResponse response = null; try { response = client.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { HttpEntity entity = response.getEntity(); String body = IOUtils.toString(entity.getContent(), "UTF-8"); System.out.println(body); EntityUtils.consume(entity); } catch (IOException e) { e.printStackTrace(); } client.close(); }
I can submit the POST request (going through security certificates), but the servlet who redirects, tells me that i need enable cookies to log in.
Analyzing the returned headers from method, that API store the cookies in headers named "Set-Cookie", so i suppose that cookies are enabled.
Can anyone help me with that?