JAVA操作Cookie工具类

Java Cookie工具类代码

  1. 添加cookie(设置有效时间)
  2. 检索所有cookie 封装到map集合 以其cookie name作为key cookie value作为value
  3. 通过cookie name 获取 cookie value
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieTool {
    /**
     * 添加cookie
     * 
     * @param response
     * @param name
     * @param value
     * @param maxAge 有效时间
     */
    public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        cookie.setDomain(".189.cn"); // cookie作用域
        response.addCookie(cookie);
    }

    /**
     * 检索所有cookie 封装到map集合 以其cookie name作为key cookie value作为value
     * 
     * @param request
     * @return
     */
    public static Map<String, String> ReadCookieMap(HttpServletRequest request) {
        Map<String, String> cookieMap = new HashMap<String, String>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie.getValue());
            }
        }
        return cookieMap;
    }

    /**
     * 通过cookie name 获取 cookie value
     * 
     * @param request
     * @param name
     * @return
     */
    public static String getCookieValueByName(HttpServletRequest request, String name) {
        Map<String, String> cookieMap = ReadCookieMap(request);
        if (cookieMap.containsKey(name)) {
            String cookieValue = (String) cookieMap.get(name);
            return cookieValue;
        } else {
            return null;
        }
    }

}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import  java.io.IOException;
import  java.util.HashMap;
import  java.util.Map;
 
import  javax.servlet.http.Cookie;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
 
public  class  CookieUtils {
     
     public  static  final  int  COOKIE_MAX_AGE =  7  24  3600 ;
     public  static  final  int  COOKIE_HALF_HOUR =  30  60 ;
       
     /**
      * 根据Cookie名称得到Cookie对象,不存在该对象则返回Null
     
      * @param request
      * @param name
      * @return
      */ 
     public  static  Cookie getCookie(HttpServletRequest request, String name) { 
         Cookie[] cookies = request.getCookies(); 
         if  (StrUtils.isEmptyArray(cookies)) { 
             return  null
        
         Cookie cookie =  null
         for  (Cookie c : cookies) {
             if  (name.equals(c.getName())) { 
                 cookie = c; 
                 break
            
        
         return  cookie; 
     }
     
     /**
      * 根据Cookie名称直接得到Cookie值
     
      * @param request
      * @param name
      * @return
      */ 
     public  static  String getCookieValue(HttpServletRequest request, String name) { 
         Cookie cookie = getCookie(request, name); 
         if (cookie !=  null ){
             return  cookie.getValue();
         }
         return  null
     }
     
     /**
      * 移除cookie
      * @param request
      * @param response
      * @param name 这个是名称,不是值
      */
     public  static  void  removeCookie(HttpServletRequest request, 
             HttpServletResponse response, String name) { 
         if  ( null  == name) { 
             return
        
         Cookie cookie = getCookie(request, name); 
         if ( null  != cookie){ 
             cookie.setPath( "/" ); 
             cookie.setValue( "" ); 
             cookie.setMaxAge( 0 ); 
             response.addCookie(cookie);
        
     }
     
     /**
      * 添加一条新的Cookie,可以指定过期时间(单位:秒)
     
      * @param response
      * @param name
      * @param value
      * @param maxValue
      */ 
     public  static  void  setCookie(HttpServletResponse response, String name, 
             String value,  int  maxValue) { 
         if  (StrUtils.isBlank(name)) { 
             return
        
         if  ( null  == value) { 
             value =  ""
        
         Cookie cookie =  new  Cookie(name, value); 
         cookie.setPath( "/" ); 
         if  (maxValue !=  0 ) { 
             cookie.setMaxAge(maxValue); 
         else 
             cookie.setMaxAge(COOKIE_HALF_HOUR); 
        
         response.addCookie(cookie);
         try  {
             response.flushBuffer();
         catch  (IOException e) {
             e.printStackTrace();
         }
     }
     
     /**
      * 添加一条新的Cookie,默认30分钟过期时间
     
      * @param response
      * @param name
      * @param value
      */ 
     public  static  void  setCookie(HttpServletResponse response, String name, 
             String value) { 
         setCookie(response, name, value, COOKIE_HALF_HOUR); 
    
  
     /**
      * 将cookie封装到Map里面
      * @param request
      * @return
      */
     public  static  Map<String,Cookie> getCookieMap(HttpServletRequest request){ 
         Map<String,Cookie> cookieMap =  new  HashMap<String,Cookie>();
         Cookie[] cookies = request.getCookies();
         if (!StrUtils.isEmptyArray(cookies)){
             for (Cookie cookie : cookies){
                 cookieMap.put(cookie.getName(), cookie);
             }
         }
         return  cookieMap;
     }
     
     
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
httputil是一个常用的网络请求工具类,用于发送HTTP请求并获取响应。在使用httputil工具类发送请求时,可以通过设置请求的cookie来实现身份验证、会话管理等功能。 要设置cookie,首先需要创建一个HttpClient对象。HttpClient用于发送请求并获取响应。在创建HttpClient对象时,可以通过HttpClientBuilder类来设置一些自定义的配置,包括cookie的相关设置。 1. 创建HttpClient对象: ```java HttpClient httpClient = HttpClientBuilder.create().build(); ``` 2. 创建HttpPost或HttpGet对象,设置请求URL和其他相关参数。 3. 设置cookie: ```java CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("cookie_name", "cookie_value"); cookie.setDomain("example.com"); cookie.setPath("/"); cookieStore.addCookie(cookie); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); ``` 这里创建了一个CookieStore对象,用于存储cookie。然后创建一个BasicClientCookie对象,设置cookie的名称和值。可以通过setDomain()和setPath()方法设置cookie的域和路径。将cookie添加到cookieStore中。 然后创建一个HttpContext对象,并将cookieStore设置为其属性。将HttpContext对象传递给HttpClient对象的execute()方法,执行请求。 4. 发送请求: ```java HttpResponse response = httpClient.execute(httpPost, httpContext); ``` 通过以上步骤,就可以使用httputil工具类发送带有cookie的HTTP请求了。在发送请求时,服务器将根据提供的cookie进行身份验证或会话管理。 需要注意的是,htttputil工具类的具体使用方式可能会因具体的框架和版本而有所不同,可以根据实际情况进行相应的调整。以上是一个基本的示例,供参考使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值