集册 Java实例教程 执行CookiePolicy

执行CookiePolicy

欢马劈雪     最近更新时间:2020-01-02 10:19:05

582
执行CookiePolicy

import java.net.*;
/**来自 
 n o w    j a v a  . c o m**/


public class BlacklistCookiePolicy implements CookiePolicy {

    String[] blacklist;


    public BlacklistCookiePolicy(String[] list) {

        blacklist = list;

    }


    public boolean shouldAccept(URI uri, HttpCookie cookie) {

        String host;

        try {

            host = InetAddress.getByName(uri.getHost())

                    .getCanonicalHostName();

        } catch (UnknownHostException e) {

            host = uri.getHost();

        }


        for (int i = 0; i < blacklist.length; i++) {// 来 自 n o w j a   v  a . c o m - 时  代  Java

            if (HttpCookie.domainMatches(blacklist[i], host)) {

                return false;

            }

        }


        return CookiePolicy.ACCEPT_ORIGINAL_SERVER

                .shouldAccept(uri, cookie);

    }

}


展开阅读全文