By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,180 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,180 IT Pros & Developers. It's quick & easy.

Java Post method - Cookie Issue

freddieMaize
P: 85
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,

Expand|Select|Wrap|Line Numbers
  1.             PrintWriter out = new PrintWriter(new FileWriter("GSA Report Log.txt"));
  2.  
  3.             String ipAddress = "******";               //give the ipadress here
  4.             int portNo = 8000;                     //give the port number here
  5.  
  6.             String path = "";                    //give the path(url)
  7.             String data = "";                    //give the inpurt parameters
  8.             URL url = null;
  9.             URLConnection conn = null;
  10.             OutputStreamWriter wr = null;
  11.             BufferedReader rd = null;
  12.  
  13.             StringBuffer sbTopKeywords = new StringBuffer();
  14.             ArrayList list_TopKeywords = new ArrayList();
  15.  
  16. //Process 1 - Login to the Appliance
  17. //The Requested URL : https://ipAddress:portNo/EnterpriseController?actionType=reload&lastcmd=login
  18.             System.out.println("********************  Start of Porcess 1 ********************");
  19.             path = "/EnterpriseController";
  20.             data = "userName=" + URLEncoder.encode("******", "UTF-8") +
  21.                     "&password=" + URLEncoder.encode("****", "UTF-8");
  22.             url = new URL("http://" + ipAddress +":"+ portNo + path);
  23.             conn = url.openConnection();
  24.             conn.setDoOutput(true);
  25.             wr = new OutputStreamWriter(conn.getOutputStream());
  26.             wr.write(data);
  27. System.out.println(data);
  28. System.out.println(wr.toString());
  29.             wr.flush();
  30.             rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  31.             String line;
  32.             while ((line = rd.readLine()) != null) {
  33.                 System.out.println(line);
  34.             }
  35.             System.out.println("********************  End of Porcess 1 ********************");
  36.  
  37.             wr.close();
  38.             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,
Expand|Select|Wrap|Line Numbers
  1. Access to this system requires that you allow cookies to be set on your computer.  Please enable cookies in your
  2. browser and hit reload twice. You can find your cookie settings under "Tool
  3. s -> Internet Options" for Internet Explorer and "Edit -> Preferences&
  4. quot; for Netscape.
Expand|Select|Wrap|Line Numbers
  1.             while ((line = rd.readLine()) != null) {
  2.                 System.out.println(line);
  3.             }
The print statement, i'm getting null

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

fREDDIE
Apr 2 '09 #1
Share this Question
Share on Google+
2 Replies


mschenkelberg
P: 44
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.setRequestProperty("User-Agent", "user agent string"); . Maybe look into this.

Max
Apr 15 '09 #2

Expert 100+
P: 392
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.
Apr 29 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.