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().