Results 1 to 2 of 2
  1. #1
    JugheadJones is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Post Working with HttpsURLConnection

    Hi,

    Im using the code below to connect to a https server from java .
    It validates the username and password but it doesnt redirect or goto the homepage when the inputstream is read
    DataInputStream input = new DataInputStream( connection.getInputStream() );

    always has the same html code from the login page even after the login has been successful .(I was expecting the html page of the user's "home").

    I tried opening the connection to a new url(the actual servlet running home page of the user) but it didnt go through..(It again gave back the html code of the login page)

    Could you please help me figure out a way to navigate through the website.
    From login page to home page to page 2 etc using java https ?

    Please find the source code below.
    Greatly appreciate the help.

    Thanks,
    JJ

    Source code:

    Java Code:
    import java.io.DataInputStream;   
    import java.io.DataOutputStream;   
    import java.net.URL;   
    import java.net.URLEncoder;   
    import java.util.Properties;   
    
    import com.sun.net.ssl.HttpsURLConnection;   
    
    public class test {   
    	public static void main(String[] args){   
    
    		try {   
    			Properties sysProperties = System.getProperties();   
    			sysProperties.put("proxy_type", "4");   
    			sysProperties.put("proxyHost", "..");   
    			sysProperties.put("proxyPort", "8080");   
    			sysProperties.put("proxySet", "true");   
    
    
    
    			System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");   
    			java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());   
    
    			URL url = new URL("..");   
    
    
    
    
    
    			HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();   
    			connection.setRequestMethod("POST");   
    			System.out.println("the opencon was successful");   
    			connection.setDoOutput(true);   
    			connection.setDoInput(true);   
    			System.out.println("doip doop --yes");   
    			connection.setUseCaches(false);   
    			connection.setDefaultUseCaches(false);   
    			String query = "j_username=" + URLEncoder.encode("..");   
    			query += "&";   
    			query += "j_password=" + URLEncoder.encode("..");   
    
    
    			connection.setAllowUserInteraction(true);   
    
    
    
    			connection.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");   
    			connection.setRequestProperty("Content-Length", "" + query.length());   
    
    
    			connection.setRequestMethod("POST");   
    
    			HttpsURLConnection.setFollowRedirects(true);   
    			connection.setInstanceFollowRedirects(true);   
    
    			connection.connect();   
    
    
    
    
    
    			// open up the output stream of the connection   
    			DataOutputStream output = new DataOutputStream( connection.getOutputStream() );   
    
    
    
    			String content =query;   
    			output.writeBytes (content);   
    			output.flush ();   
    
    
    
    
    
    			System.out.println("Resp Code:"+connection.getResponseCode());   
    			System.out.println("Resp Message:"+ connection.getResponseMessage());   
    
    
    			output.close();   
    
    
    			DataInputStream input = new DataInputStream( connection.getInputStream() );   
    
    			//read in each character until end-of-stream is detected   
    			for( int c = input.read(); c != -1; c = input.read() )   
    				System.out.print( (char)c );   
    			input.close();   
    
    
    
    
    		}   
    		catch(Exception e)   
    		{   
    			System.out.println( e );   
    			e.printStackTrace();   
    		}   
    	}   
    }
    Last edited by JugheadJones; 05-11-2010 at 02:18 PM. Reason: Formatting code

  2. #2
    dongorangoran is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default cookies

    hi,

    i have the same problem...check out if the page you want login need cookies if yes you have to simulate the cookie saving ...

Similar Threads

  1. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 11:22 PM
  2. buttons not working
    By daud in forum New To Java
    Replies: 2
    Last Post: 08-16-2009, 12:04 AM
  3. My program is not working
    By MICHAELABICK in forum New To Java
    Replies: 6
    Last Post: 12-22-2008, 11:05 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 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
  •