JavaSe: Cookie management API introduction, javasecookie

Source: Internet
Author: User

JavaSe: Cookie management API introduction, javasecookie

CookieManager

There is no Cookie Management in HttpURLConnection. How can I manage cookies when using Java programs?

Cookie case

 

1. User Agent -> ServerPOST /acme/login HTTP/1.1[form data]2. Server -> User AgentHTTP/1.1 200 OKSet-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"3. User Agent -> ServerPOST /acme/pickitem HTTP/1.1Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"[form data]

 

Two Cookie forms

SessionCookie

When using the applet and application, the Session Cookie is stored in the memory. When the program exits, the cookie is automatically deleted. The session id is saved in the Cookie, so that you do not need to log on again when switching between different pages of the same site.

 

PersistenceCookie

Unlike Session Cookie, when an application exits, the cookie will not be deleted until it expires.

Access cookies through programming

Set before request sendingCookie

When sending an HTTP request, you can use URLConnection. setRequestProperty (String key, String value) to set the Cookie.

URL url = new URL( "http://java.sun.com/" );URLConnection conn = url.openConnection(); conn.setRequestProperty("Cookie", "foo=bar"); InputStream is = conn.getInputStream(); ......

 


View after receiving the response
Cookie 

After receiving the response, you can use URLConnection. getHeaderFields () to view the Cookie:

URL url = new URL( "http://java.sun.com/" ); URLConnection conn = url.openConnection(); conn.connect(); ..... Map<String, List<String>> headers = conn.getHeaderFields(); List<String> values = headers.get("Set-Cookie"); String cookieValue = null; for (Iterator iter = values.iterator(); iter.hasNext(); ) {String v = values.next(); if (cookieValue == null)cookieValue = v;elsecookieValue = cookieValue + ";" + v;} 

 

 

CookieHandler

These two methods are common. In fact, there is another dedicated Cookie management method: CookieHandler.

 

Public abstract Map <String, List <String> get (URI uri, Map <String, List <String> requestHeaders) throws IOExceptionGets all the applicable cookies from a cookie cache for the specified uri in the request header. HTTP protocol implementers shocould make sure that this method is called after all request headers related to choosing cookies are added, and before the request is sent. this method is used from the Cookie Cache (that is, Coo KieStore) to obtain all the cookies that can be used. The implementation of the HTTP protocol should ensure that this method is called after the Cookie request header is set and before the request is sent.

From the description of the method, this is the callback function. This method will be used in HTTPURLConnection. after setRequestProperty ("Cookie", "xxxx = yyy") is executed. it is automatically called before connect () and does not need to be called in the code.

The returned value of this method is a Map. The data in the returned value is sent to the server as part of the request header. Corresponds to the (3) process in the case.

 

Correspondingly, the put method sets the Cookie in the response header to the Cookie Cache of the client.

The default response header returned by the server uses set-cookie as the identifier of the Cookie to be set.

After the client (such as a browser) receives a response, it uses put () to set the data in set-cache to the Cookie Cache of the client. Corresponds to the (2) process in the case.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.