Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Log in using Apache HttpComponents

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Log in using Apache HttpComponents

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Log in using Apache HttpComponents

    Do you have a full class with import statements that can be compiled and executed for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Log in using Apache HttpComponents

    Quote Originally Posted by Norm View Post
    Do you have a full class with import statements that can be compiled and executed for testing?
    package javaweb;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.net.ssl.KeyManager;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
     
    import org.apache.commons.io.IOUtils;
    import org.apache.http.Consts;
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpVersion;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.params.ClientPNames;
    import org.apache.http.client.params.CookiePolicy;
    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.conn.scheme.Scheme;
    import org.apache.http.conn.scheme.SchemeRegistry;
    import org.apache.http.conn.ssl.SSLSocketFactory;
    import org.apache.http.impl.client.BasicCookieStore;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.SingleClientConnManager;
    import org.apache.http.impl.cookie.BasicClientCookie;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.params.CoreProtocolPNames;
    import org.apache.http.util.EntityUtils;
     
    @SuppressWarnings("deprecation")
    public class LoginJavaWeb {
     
    	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);
     
    		//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();
    	}
     
    	private static class DefaultTrustManager implements X509TrustManager {
     
    	       public void checkClientTrusted1(X509Certificate[] xcs, String string) throws CertificateException {
    	       }
     
    	       public void checkServerTrusted1(X509Certificate[] xcs, String string) throws CertificateException {
    	       }
     
    	       public X509Certificate[] getAcceptedIssuers() {
    	           return null;
    	       }
     
    	       public void checkClientTrusted(X509Certificate[] chain, String authType)
    					throws CertificateException {
    			}
     
    			public void checkServerTrusted(X509Certificate[] chain, String authType)
    					throws CertificateException {
    			}
     
    	   }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Log in using Apache HttpComponents

    Sorry, I didn't recognize all those non-java SE classes. I can't compile and test the code without them. I missed the meaning of the title.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: January 28th, 2013, 05:29 AM
  2. Replies: 1
    Last Post: November 1st, 2012, 07:59 AM
  3. Log Out Problem
    By Rituparna in forum Java Servlet
    Replies: 4
    Last Post: March 15th, 2011, 08:32 AM
  4. [SOLVED] events log
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 17th, 2010, 05:21 AM
  5. Log Management
    By Sai in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2010, 11:11 AM