I am writing an application that retrieves a URL, but that URL requires a cookie. Is there a way to hold that cookie within my application and send it back to the URL when requested?

Tim Rohaly

A cookie is set by the server by sending a special HTTP header (Set-Cookie) in response to a client's HTTP request. The client may read this header and store the cookie locally either on disk or in memory. Subsequent client HTTP requests may send the cookie back to the server, under certain conditions. This interaction is explained well in many places, see for example http://www.cookiecentral.com/faq/.

If you want to write your own client application that understands cookies, you should use the getHeaderField() method of URLConnection to obtain any cookies sent by the server, store them in memory or on disk, then send them back to the server when needed with the setRequestProperty() method.