java.net.CookiePolicy
CookiePolicy implementations decide which cookies should be acceptedand which should be rejected. Three pre-defined policy implementationsare provided, namely ACCEPT_ALL, ACCEPT_NONE and ACCEPT_ORIGINAL_SERVER.
Declaration
public interface CookiePolicy {public static final CookiePolicy ACCEPT_ALL = new CookiePolicy() {public boolean shouldAccept(URI uri, HttpCookie cookie) {return true;}};public static final CookiePolicy ACCEPT_NONE = new CookiePolicy() {public boolean shouldAccept(URI uri, HttpCookie cookie) {return false;}};public static final CookiePolicy ACCEPT_ORIGINAL_SERVER = new CookiePolicy() {public boolean shouldAccept(URI uri, HttpCookie cookie) {if (uri == null || cookie == null)return false;return HttpCookie.domainMatches(cookie.getDomain(), uri.getHost());}};public boolean shouldAccept(URI uri, HttpCookie cookie);}
Methods
1. shouldAccept(URI uri, HttpCookie cookie)
boolean java.net.CookiePolicy.shouldAccept(URI uri, HttpCookie cookie)
Will be called to see whether or not this cookie should be accepted.
Parameters: Two parameters are required for this method.
URI: the URI to consult accept policy with.
cookie: the HttpCookie object in question.
Returns: true if this cookie should be accepted;otherwise, false.
Approach
Java
package com.CookiePolicy;import java.net.CookiePolicy;import java.net.HttpCookie;import java.net.URI;import java.net.URISyntaxException;public class CookiePolicy1 {public static void main(String[] args) throws URISyntaxException {CookiePolicy cookiePolicy = CookiePolicy.ACCEPT_ALL;URI uri = new URI("https://beingcodeexpert.blogspot.com/");HttpCookie httpCookie = new HttpCookie("domain", "value");System.out.println(cookiePolicy.shouldAccept(uri, httpCookie));}}
Output:
true
2. ACCEPT_ALL
CookiePolicy java.net.CookiePolicy.ACCEPT_ALL
One pre-defined policy which accepts all cookies.
3. ACCEPT_NONE
CookiePolicy java.net.CookiePolicy.ACCEPT_NONE
One pre-defined policy which accepts no cookies.
4. ACCEPT_ORIGINAL_SERVER
CookiePolicy java.net.CookiePolicy.ACCEPT_ORIGINAL_SERVER
One pre-defined policy which only accepts cookies from original server.
Approach
Java
package com.CookiePolicy;import java.net.CookiePolicy;import java.net.HttpCookie;import java.net.URI;import java.net.URISyntaxException;public class CookiePolicy1 {public static void main(String[] args)throws URISyntaxException {System.out.println(CookiePolicy.ACCEPT_ALL);System.out.println(CookiePolicy.ACCEPT_NONE);System.out.println(CookiePolicy.ACCEPT_ORIGINAL_SERVER);CookiePolicy cookiePolicy = CookiePolicy.ACCEPT_ALL;URI uri = new URI("https://beingcodeexpert.blogspot.com/");HttpCookie httpCookie = new HttpCookie("domain", "value");System.out.println(cookiePolicy.shouldAccept(uri, httpCookie));}}
Output:
java.net.CookiePolicy$1@2401f4c3 java.net.CookiePolicy$2@41a4555e java.net.CookiePolicy$3@3830f1c0 true
Some other classes/interfaces of java.net
Authenticator: The class Authenticator represents an object that knows how to obtain authentication for a network connection.
BindException: Signals that an error occurred while attempting to bind a socket to a local address and port.
CacheRequest: Represents channels for storing resources in the ResponseCache.
CacheResponse: Represent channels for retrieving resources from the ResponseCache.
ConnectException: Signals that an error occurred while attempting to connect a socket to a remote address and port.
ContentHandler: The abstract class ContentHandler is the superclass of all classes that read an Object from a URLConnection.
ContentHandlerFactory: This interface defines a factory for content handlers. Implementing this interface should map a MIME type into an instance of ContentHandler.
CookieHandler: A CookieHandler object provides a callback mechanism to hook up an HTTP state management policy implementation into the HTTP protocol handler.
CookieManager: CookieManager provides a concrete implementation of CookieHandler, which separates the storage of cookies from the policy surrounding accepting and rejecting cookies.
CookiePolicy: CookiePolicy implementations decide which cookies should be accepted and which should be rejected.
CookieStore: A CookieStore object represents storage for cookies. Can store and retrieve cookies.
DatagramPacket: This class represents a datagram packet.
No comments:
Post a Comment