Java Post method - Cookie Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freddieMaize
    New Member
    • Aug 2008
    • 85

    Java Post method - Cookie Issue

    Hi everyone,

    I'm trying to access a url from java (using java post method). The url is a login page. I then insert values (username and password) to it an try to post that page. Below is my code,

    Code:
                PrintWriter out = new PrintWriter(new FileWriter("GSA Report Log.txt"));
    
                String ipAddress = "******";               //give the ipadress here
                int portNo = 8000;	                 //give the port number here
    
                String path = "";                    //give the path(url)
                String data = "";                    //give the inpurt parameters
                URL url = null;
                URLConnection conn = null;
                OutputStreamWriter wr = null;
                BufferedReader rd = null;
    
                StringBuffer sbTopKeywords = new StringBuffer();
                ArrayList list_TopKeywords = new ArrayList();
    
    //Process 1 - Login to the Appliance
    //The Requested URL : https://ipAddress:portNo/EnterpriseController?actionType=reload&lastcmd=login
                System.out.println("********************  Start of Porcess 1 ********************");
                path = "/EnterpriseController";
                data = "userName=" + URLEncoder.encode("******", "UTF-8") +
                        "&password=" + URLEncoder.encode("****", "UTF-8");
                url = new URL("http://" + ipAddress +":"+ portNo + path);
                conn = url.openConnection();
                conn.setDoOutput(true);
                wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
    System.out.println(data);
    System.out.println(wr.toString());
                wr.flush();
                rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = rd.readLine()) != null) {
                    System.out.println(line);
                }
                System.out.println("********************  End of Porcess 1 ********************");
    
                wr.close();
                rd.close();
    I get a error saying I need to enable cookie in my browser. But even after doing so it is still throwing me the same error. Below is the error message,
    Code:
    Access to this system requires that you allow cookies to be set on your computer.  Please enable cookies in your
    browser and hit reload twice. You can find your cookie settings under "Tool
    s -> Internet Options" for Internet Explorer and "Edit -> Preferences&
    quot; for Netscape.
    Code:
                while ((line = rd.readLine()) != null) {
                    System.out.println(line);
                }
    The print statement, i'm getting null

    Any thoughts on this please do share. Thanks a lot :)

    fREDDIE
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    usually cookie checking is done using client side javascript, the site might be checking to see if you are a valid browser by checking the user-agent property and seeing if it is a known browser. I'm not sure though, but I know it is possible to "pretend" to be a valid browser like firefox by doing something like url.setRequestP roperty("User-Agent", "user agent string"); . Maybe look into this.

    Max

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      You will spend a huge amount of time trying to work out all the details of emulating a HTTP client your self. You would be a lot better of using one of the library's for this. Like HttpClient lib (http://hc.apache.org/httpclient-3.x/) from Apache.

      Comment

      Working...