Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Cookies and Java

Thread view: 
jack.smith.sam@gmail.com - 04 Oct 2006 06:35 GMT
Hi All,

I want to get data from URLURL using Java. If I use wget as the
following, I will get the result:

wget --user-aget=Mozilla/5.0 --cookies=on "URLURL"

So I tried to use cookies in Java but can not figure out how. Here is
how I try to do that:

        String Url="URLURL";
        URL url = new URL(Url);

        java.net.URLConnection conn = url.openConnection();
        conn.setRequestProperty("User-Agent","Mozilla/5.0");
        String myCookie = "";
        conn.setRequestProperty("Cookie", myCookie);
        conn.connect();
but it does not work.
Can you help me?

thanks a lot for you time.
Andrew Thompson - 04 Oct 2006 07:50 GMT
..
> but it does not work.

Wait, I know this one..

> Can you help me?

If it is an IAmJustFeelingLazyException, you might
try a call to soundFlogging() immediately prior to the
code shown.

If it is something else, you might try.. something else,
like - being very specific about how the code is not working
(ensuring you make clear what you expected to happen).

It is best to ..
- not swallow exceptions (is was unclear from your code snippet)
- copy/paste compile & runtime exceptions and errors
(stop at 50+ lines of stack trace).

Andrew T.
Chris Uppal - 04 Oct 2006 09:44 GMT
> java.net.URLConnection conn = url.openConnection();
> conn.setRequestProperty("User-Agent","Mozilla/5.0");
> String myCookie = "";
> conn.setRequestProperty("Cookie", myCookie);
> conn.connect();

I doubt if you should be opening the connection until after you've set the
cookies and what-have-you.

   -- chris
jack.smith.sam@gmail.com - 04 Oct 2006 10:07 GMT
Thanks Chris.
I used HttpClient class and it solved my problem.

> > java.net.URLConnection conn = url.openConnection();
> > conn.setRequestProperty("User-Agent","Mozilla/5.0");
[quoted text clipped - 6 lines]
>
>     -- chris
Tor Iver Wilhelmsen - 04 Oct 2006 19:29 GMT
> I doubt if you should be opening the connection until after you've set the
> cookies and what-have-you.

Then what should he set the cookies etc. in? Notice that the method
used to set request headers (setRequestProperty()) is in
URLConnection, which is reutrned by openConnection().


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.