返回顶部

收藏

【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. 博主 发表 2016-09-16 01:49:16 cookie 的一点研究
  2. 博主 发表 2016-10-22 04:19:18 cookie 的一点研究
  3. 博主 发表 2015-05-13 07:14:14 SuperAgent使用
  4. 果冻想 发表 2015-12-26 15:39:44 HTTP Cookie学习笔记
  5. Panni_007 发表 2013-04-09 07:46:57 http-only型cookie截取及利用
  6. Novtopro He 发表 2015-04-13 08:52:29 HTTP cookies
  7. viviworld 发表 2015-03-06 10:01:15 为前端 web 开发者准备的 HTTP2
  8. 全 恒壮 发表 2011-05-29 11:39:12 判断Keep-Alive模式的HTTP请求的结束
  9. admin 发表 2013-03-21 04:20:23 Cookie:一半是广告,一半是隐私
  10. simaopig 发表 2013-04-17 15:33:21 HTTP 正向代理与反向代理
  11. 博主 发表 2011-07-21 16:00:00 Abort HTTP 请求
  12. 博主 发表 2012-02-08 08:00:00 Nginx-Lua HTTP 401 认证校验

发表评论