aspose file tools*
The moose likes Java in General and the fly likes Java cookies ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java cookies ?" Watch "Java cookies ?" New topic
Author

Java cookies ?

J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
first of all, im telling you what i want to do:

1) give my program a page to go to and i want it to save some information to database.

2) i have the database working, im using HSQLDB, now i have a script that takes all information(source code) from html and makes it as a string

3) the problem is, when my script goes to website it isn't allowed to see certain content and it always throws me to login page. even when i browse for example in chrome or IE and im logged in to site. when i go with java script, then it is never logged in.

4) so i think i need to make some cookies with java, meaning that java logs in the site and then takes the source code of certain pages.

my script for taking source code from page:




all i need now, is how to make cookie for logging in.. i googled and stuff but i am not sure where to start. all i need it to do is to log in with my username and pw..and active my script
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Check out the Apache HttpClient library -which knows how to handle cookies- or, even better because it works on a higher level, the jWebUnit library. In my opinion, it's the best library for programmatic HTTP access that exists in the Java space.


Ping & DNS - updated with new look and Ping home screen widget
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
Ulf Dittmer wrote:Check out the Apache HttpClient library -which knows how to handle cookies- or, even better because it works on a higher level, the jWebUnit library. In my opinion, it's the best library for programmatic HTTP access that exists in the Java space.


i googeld that jWebUnit, as it seems its like JUnit but for web applications, so its only for testing?

ive had just a tiny bit experience with JUnit and it was rather useless imo, only for somekind of testing maybe useful ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
i googeld that jWebUnit, as it seems its like JUnit but for web applications, so its only for testing?

No. Just ignore everything it says about unit testing - it's a general-purpose web client library.
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
Ulf Dittmer wrote:
i googeld that jWebUnit, as it seems its like JUnit but for web applications, so its only for testing?

No. Just ignore everything it says about unit testing - it's a general-purpose web client library.


well at first HttpClient seemed reasonable so i went for this, so now i have started with a tutorial and i have a problem.

first of all i went to HTTPClient homepage and read tutorial from there, but everything seemed to be outdated there..some commands didn't work
http://hc.apache.org/httpclient-3.x/tutorial.html

commands that did not work:
1) HttpClient client = new HttpClient(); <<------ i changed it with HttpClient httpclient = new DefaultHttpClient();
2)HttpMethod method = new GetMethod("http://www.apache.org/"); <--------- changed this with HttpGet httpget = new HttpGet("http://localhost/");

etc.etc
so i started with new tutorial that seemed to work

http://docs.google.com/viewer?a=v&q=cache:uNl5BB6U3FkJ:hc.apache.org/httpcomponents-client-ga/tutorial/pdf/httpclient-tutorial.pdf+httpclient+tutorial&hl=et&gl=ee&pid=bl&srcid=ADGEESgp--eZC2oGiN0lj-OHnV5qqarowTE6bec0VU-2eaOLvwgYSlzz5uPvbZpbXkCoghJ3j0fjt1MZn50C1R7QVCfVu4lbFWBTqovLKFUMDPrey3-mmP-oJw4Pi9DW-tNR6c9femLQ&sig=AHIEtbQ3NLZgqg3RKSjQUQNEFe04vvlCZw

but now i have go to the point where no tutorial works:
i get an error, and i have no idea how to handle it..
this is from the tutorial, that is on the HttpClient site.

so i click on that "build path" and i have no idea what to do





and this is the tutorial from the other site



Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

You don''t seems to have imported *all* the required classes as you have only three import statements in the code?
And Welcome to JavaRanch


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
In addition to importing all classes, you need to keep *all* jar files in the classpath. It looks like you're missing two of the ones that come with the httpcomponents-client download (apache-mime4j and httpcore).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Off-topic, but worth mentioning: A build tool like Maven is great to spare you from doing all this dependency management manually. You just tell Maven that you need Apache HttpClient and it will download and use all the dependencies for you automatically.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
Ulf Dittmer wrote:In addition to importing all classes, you need to keep *all* jar files in the classpath. It looks like you're missing two of the ones that come with the httpcomponents-client download (apache-mime4j and httpcore).


well i had only 2 jar files with httpcomonents.


and as i googeled HttpResponse i saw it should come from the httpclient.jar file

http://www.innovation.ch/java/HTTPClient/api/HTTPClient/HTTPResponse.html

+--HTTPClient.HTTPResponse


i hope i downloaded right httpclient..i downloaded from here : http://mirror.cc.columbia.edu/pub/software/apache//httpcomponents/httpclient/binary/httpcomponents-client-4.0.3-bin.zip
or i should get one with osgi bundle? is osgi bundle, theres only 1 jar file
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

This page lists the dependencies. Besides the httpclient jar, you need commons-codec and commons-logging (junit is only needed for testing). Download those libraries and also put them in your classpath.

Note that the download page also offers a "Binary with dependencies" package. That contains the httpclient jar together with the dependencies that it needs.

Getting the OSGi bundle will not help (it's really only useful when you want to deploy it as a module in an OSGi container).
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
Jesper de Jong wrote:This page lists the dependencies. Besides the httpclient jar, you need commons-codec and commons-logging (junit is only needed for testing). Download those libraries and also put them in your classpath.

Note that the download page also offers a "Binary with dependencies" package. That contains the httpclient jar together with the dependencies that it needs.

Getting the OSGi bundle will not help (it's really only useful when you want to deploy it as a module in an OSGi container).


but look my post with lots of pictures, there you can see that i have all the paths..and also in the left in package explorer there are jars in lib

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
The two libraries I mentioned are part of the HttpCore package. Both packages (Client and Core) can be downloaded from http://hc.apache.org/downloads.cgi.

Or -as Jesper said- you can get the "binaries with dependencies" package that includes all the dependencies.
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
okay getting the core jars worked..ill keep on workin on this thing
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
sorry for double post but new problem occured.

every JAR is added, also added all of them to configuration build path.

now i get this errors. i googeled around abi, but still not sure what to do, i tried to add like a enviromental variable as classpath to the httpcore, that didnt't work, if i did it right.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

You have a compiler error somewhere. Solve that first before running your application.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
Rob Prime wrote:You have a compiler error somewhere. Solve that first before running your application.


okay great, got it working now, what it was, was that i moved around the jar files in lib folder, but forgot to remove build paths from configuration
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18



well i'm testing out this code with a browser based game. PokerRPG .
i got this code from google and it kinda of works, well it does print all the source code, but the problem is that, it take the source code of the main site, so apparently it does not log in? any suggestions ?


J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
okay, seems like noone has experience with such specific and advanced stuff. so ill try using jwebunit

i downloaded it and im trying to get the first example code to work, but i have no idea what im doing wrong.
i did as it told me :
http://jwebunit.sourceforge.net/installation.html
...make sure you add the jwebunit-htmlunit-plugin-XX.jar, jwebunit-core-XX.jar and all HtmlUnit dependencies (these files are in lib/ folder of the JWebUnit package) to your classpath in your IDE.


i did this as you can see from left...why it still gives me error? i googeled it, but i seem to have done everything
im using this tutorial, the first code example
http://jwebunit.sourceforge.net/quickstart.html#Validating_Table_Content



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
seems like noone has experience with such specific and advanced stuff.

If you came to this conclusion by the fact that a post of yours went unanswered for a mere 9 hours, then be advised that a major holiday is coming up in large parts of the world, and people may have other priorities. Also note that the URL in line 31 is obviously bogus.

The class you're missing is part of JUnit, which is not listed in the jar files shown in the screenshot. I'm not sure if it comes with JWebUnit; you may have to download it separately.
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
weird..it gave me normal source code before..everything..with paragraphs etc etc..now its just throwing me source code and weirdest things ever.
btw if you copy this all to html file, you can see, its still in loggin window, so that didn't fix anything
anyways im giving a shot to jwebunit for a while



i imported the junit to the project now, it works now.


J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
So anyways:

now i trying to get mu jwebunit to work and i've run into next problem.


one assert works that i made up myself.
but others...im just using a random assert name, so the assert should return true or failure. but it gives me error.


and this picture is of the left bottom corner, the failure trace..i also googled nullpointerexception on jwebunit, did not find what i need apparently


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

You might want to check this thread (and perhaps some other threads too...).
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
Vijitha Kumara wrote:You might want to check this thread (and perhaps some other threads too...).


okay thanks, i got what i needed, i needed to add a starting point



also it works good now..but is it normal that it prints ton of code? since i do not have a System.out.println in my code, but maybe its in those jwebunit jar files.

this is what output i get, just wanna know if its normal, and does this also for you guys?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Looks like the logging level is set to DEBUG; if you push that up to INFO you should get a lot fewer messages, if any.
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
1) yeah seems likes so, but i don't have any xml files, so i can't change the log level. well i have one xml file, but it was for database

where i can change the log level? must i alter the sourcecode of jar file?

this is what i tried from inside java code..
did not work.still is on debug level



2) also how can use jwebunit without the testing..i was lookin for different code examples, but theres always some testmethods.
when i comment the testmethods out, it just says, there are no tests found in jweb, so that doesn't help
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Logging in HtmlUnit... I really haven't tried it though.
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
If you don't explicitly configure commons logging to use LOG4J or another logging framework then it will use the simple logger. When using the simple logger, you can change the default logging level by setting the following system property.


with that said it SHOULD use the simple logger.

so adding this line from that link, should work





unfortunatelly it still give me million lines of log. and its still DEBUG log

so eventually i tried importing this address, if it exists, but it seems it does not...no everyhting is weird now. :O



all things considered, im not even sure now what logger it uses, but i know its priority level is set to debug

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

2) also how can use jwebunit without the testing..i was lookin for different code examples, but theres always some testmethods.

It's a testing framework (an extension of JUnit for web based testing), what would you expect from it other than that?
so eventually i tried importing this address...

No. You don't need to, it just sets a system property to be used by the library. I'm not sure whether it really uses it, may need to look for some additional documentation...
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
Ulf Dittmer wrote:
i googeld that jWebUnit, as it seems its like JUnit but for web applications, so its only for testing?

No. Just ignore everything it says about unit testing - it's a general-purpose web client library.


ulf dittmer said it could work also without testing

btw.still having those debugs messages up there

weird thing is that, theres really only a short page of user documentation for jwebunit..which sucks..very basic
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

I didn't noticed that in the first place, The library comes with all those HttpClient, HttpCore etc... so yes you can have those features too...
J. Sellin
Greenhorn

Joined: Dec 22, 2010
Posts: 18
okay i was messing around with those jars and dicovered something..

first of all, i started removing my jar files to see which jar file was my program using for logging..because those DEBUG messages were lagging my program up and were just making it too slow to be useful.

finally i removed these 2 jar files from build path:


i was happy since it did remove the debug messages, but now it was giving me error messages..so i thought, this must be the jars that my program is logging with.


so i read the logback documentation and stuff, did not get help..so i put those 2 back to build path..

then i was looking at those debug logs, and what caught my eye, was this line:


so i googled org.apache.http.wire and found out that httpclient is using this..then i downloaded the source code of httpclient and found out all those debug messages do come from this Wire.java class and its using apache commons logging.

so now i dont understand, is it using the logback or commons?? either way ive tried to remove these debug messages, i mean changing it to info level, did not get anywhere, they still come...any suggestions?





Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Commons Logging is a wrapper library that can use any one of a number of logging implementations - log4j, slf4j, java.util.logging, ...

If you don't want any logging, remove all slf4j and logback related jar files, and configure Commons Logging to use the SimpleLog as described in http://commons.apache.org/logging/guide.html. This involves putting a properties file in the classpath.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java cookies ?
 
Similar Threads
Connect to ASP app via JSP and display output?
Download a file via https
Viewing HTML source problem..
Need to download a file
HttpURLConnection or URLConnection to login in to sites