Cookies and URLConnection.setRequestProperty()
I need to send a cookie to a server over HTTP. I have found several threads on how to do this, using the setRequestProperty() method of URLConnection. No luck.
I tried all kinds of spellings, like
urlConn.setRequestProperty("Cookie" , "this=that");
urlConn.setRequestProperty("Cookie:" , "this=that");
and so on, and then check it with a simple snoop servlet. No luck :-(
Does anybody know what I may be doing wrong? I do not call the connect() method of the URLConnection explicitlty or implicitly before calling setRequestProperty. Is the only solution using sockets?
#marius
Solved it!
A bit embarassing of course, but here's my solution:
For some strange reason (copy&paste is Evil), I never used my nice URLConnection object, but in stead called URL.openStream(), which of course generates a new URLConnection object that I can't manipulate :-(
So, now, this code works:
URL myURL = new URL("http://www.somewhere.com");
URLConnection urlConn = myURL.openConnection();
urlConn.setRequestProperty("MyCookie","Something");
InputStream inStream = urlConn.getInputStream();
//Do not try InputStream inStream = myURL.openStream() here, it will mess up everything
Now, if I could only find a way to disable Ctrl-C and Ctrl-V...
//marius. Happy Man