Cookiemanager
In the use of httpurlconnection, there is no management of cookies. How do you manage cookies when you use Java programs?
Cookie case
1. User Agent-/acme/login http/1.1[form data]2. Server, User agenthttp/1.1 okset-cookie2:customer= "Wile_e_coyote"; version= "1"; Path= "/ACME" 3. User Agent-/acme/pickitem http/1.1Cookie: $Version= "1"; Customer= "Wile_e_coyote"; $Path = "/acme"[form data]
Two forms of cookies
Session Cookies
When using applets, application, the Session cookie is stored in memory and the cookie is automatically deleted when the program exits. The ID of the session is saved in the cookie so that when you switch between pages in the same site, you do not need to repeat the login.
Persistence of Cookies
Unlike session cookies, when an application exits, the cookie is not deleted and will be deleted until it expires.
Programmatic access to cookies
Pre-send request settings Cookies
When sending an HTTP request, you can set the cookie using Urlconnection.setrequestproperty (string key, String value).
New URL ("http://java.sun.com/"= url.openconnection (); Conn.setrequestproperty ("Cookie", "Foo=bar" = conn.getinputstream ();
View cookies After receiving a response
After you receive the response, you can use the Urlconnection.getheaderfields () method to view the cookie:
URL url =NewURL ("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 methods, in fact there are other special management cookie processing: Cookiehandler.
Public Abstract Map<string,list<string>> Get (URI uri,map<string,list<string>> requestHeaders) throws for this method was called after all request headers related to choosing the cookies are added, a nd before the request is sent. This method is used to obtain all available cookies from the cookie Cache (that is, Cookiestore). The implementation of the HTTP protocol should ensure that this method is called before the request is sent, after the relevant cookie request header is set.
From the description of the method, this is the callback function, which will be executed after Httpurlconnection.setrequestproperty ("Cookie", "xxxx=yyy"). It is called automatically before Httpurlconnection.connect () and does not need to be called in the code.
The return value of the method is a map, and the data in the return value is sent to the server as part of the request header. Corresponds to the (3) procedure in the case.
In this way, the Put method is to set the cookie in the response header to the client's cookie cache.
The default service side returns a response header that uses Set-cookie as the identity to set the cookie.
After the client (for example, the browser) receives the response, use put () to set the data in the Set-cache to the client's cookie cache. Corresponds to the (2) procedure in the case.
Introduction to the API for Javase:cookie management