返回顶部

收藏

【Java】CookieStore 类使用示例

更多

CookieStore 是 Java API 中用来处理 HTTP 客户端的 Cookie 存储策略的类。

WebClient.java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WebClient {
  public static void main(String[] args) throws Exception {
    CookieStore store = new MyCookieStore();
    CookiePolicy policy = new MyCookiePolicy();
    CookieManager handler = new CookieManager(store, policy);
    CookieHandler.setDefault(handler);
    URL url = new URL("http://localhost:8080/cookieTest.jsp");
    URLConnection conn = url.openConnection();

    InputStream in = conn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String input;
    while ((input = reader.readLine()) != null) {
      System.out.println(input);
    }
    reader.close();

  }
}

class MyCookiePolicy implements CookiePolicy {
  public boolean shouldAccept(URI uri, HttpCookie cookie) {
//    String host = uri.getHost();
  //  return host.equals("localhost");
    return true;
  }
}

class MyCookieStore implements CookieStore {
  private Map<URI, List<HttpCookie>> map = new HashMap<URI, List<HttpCookie>>();

  public void add(URI uri, HttpCookie cookie) {
    List<HttpCookie> cookies = map.get(uri);
    if (cookies == null) {
      cookies = new ArrayList<HttpCookie>();
      map.put(uri, cookies);
    }
    cookies.add(cookie);
  }

  public List<HttpCookie> get(URI uri) {
    List<HttpCookie> cookies = map.get(uri);
    if (cookies == null) {
      cookies = new ArrayList<HttpCookie>();
      map.put(uri, cookies);
    }
    return cookies;
  }

  public List<HttpCookie> getCookies() {
    Collection<List<HttpCookie>> values = map.values();
    List<HttpCookie> result = new ArrayList<HttpCookie>();
    for (List<HttpCookie> value : values) {
      result.addAll(value);
    }
    return result;
  }

  public List<URI> getURIs() {
    Set<URI> keys = map.keySet();
    return new ArrayList<URI>(keys);

  }

  public boolean remove(URI uri, HttpCookie cookie) {
    List<HttpCookie> cookies = map.get(uri);
    if (cookies == null) {
      return false;
    }
    return cookies.remove(cookie);
  }

  public boolean removeAll() {
    map.clear();
    return true;
  }
}

cookieTest.jsp

<%
    Cookie cookie = new Cookie ("username", "guest");

    cookie.setMaxAge (100);
    response.addCookie (cookie);
%>
<html>
<head>
<title>HttpCookie Demo</title>
</head>
<body>
Add cookie. Cookie name = <%=cookie.getName () +". Cookie value = " + cookie.getValue () %>
</body>
</html>

标签:HTTP,Cookie,CookieStore

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2015-05-13 07:14:14 SuperAgent使用
  2. 果冻想 发表 2015-12-26 15:39:44 HTTP Cookie学习笔记
  3. Novtopro He 发表 2015-04-13 08:52:29 HTTP cookies
  4. Panni_007 发表 2013-04-09 07:46:57 http-only型cookie截取及利用
  5. viviworld 发表 2015-03-06 10:01:15 为前端 web 开发者准备的 HTTP2
  6. 博主 发表 2016-09-16 01:49:16 cookie 的一点研究
  7. 博主 发表 2016-10-22 04:19:18 cookie 的一点研究
  8. admin 发表 2009-08-09 02:14:09 linux系统中wget命令使用指南
  9. linux@linux.cn (linu 发表 2015-05-17 03:12:29 Web缓存基础:术语、HTTP报头和缓存策略
  10. 脑袋 发表 2015-02-07 07:24:31 理解HTTP幂等性
  11. 涅槃 发表 2012-09-21 02:54:05 本课程网址:http://ve...
  12. Nix.Huang 发表 2015-05-22 02:10:40 基于Centos 7搭建支持HTTP协议的svn服务器

发表评论