Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Login Using Apache HttpComponents

  1. #1
    pedro.its is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Login 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:

    Java 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
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Login Using Apache HttpComponents

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    pedro.its is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Login Using Apache HttpComponents

    Quote Originally Posted by PhHein View Post
    Yes, but there's two different forums, not?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Login Using Apache HttpComponents

    Quote Originally Posted by pedro.its View Post
    Yes, but there's two different forums, not?
    If I take 10-20 minutes of my time to answer and then find out somebody already gave you the answer, I'd be well pissed off.
    gimbal2 likes this.

  5. #5
    pedro.its is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Login Using Apache HttpComponents

    Quote Originally Posted by SurfMan View Post
    If I take 10-20 minutes of my time to answer and then find out somebody already gave you the answer, I'd be well pissed off.
    For this, when i found a solution, i will post this in every forum where i did same question...

Similar Threads

  1. Login to a CAS server using HTTPComponents Client
    By marcomandy in forum Networking
    Replies: 2
    Last Post: 03-30-2012, 03:07 PM
  2. telnet login problem (apache.commons)
    By thedoubledipper in forum Networking
    Replies: 0
    Last Post: 01-20-2012, 07:13 PM
  3. Replies: 3
    Last Post: 11-05-2010, 06:55 AM
  4. How to interact with Web page? ( HttpComponents)
    By featon in forum New To Java
    Replies: 0
    Last Post: 05-28-2010, 11:58 PM
  5. login with cookies using Apache httpClient
    By MacGyver in forum Advanced Java
    Replies: 0
    Last Post: 05-15-2010, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •