Cookies and Java

J

jack.smith.sam

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.
 
A

Andrew Thompson

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.
 
C

Chris Uppal

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
 
T

Tor Iver Wilhelmsen

Chris Uppal said:
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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top