当前位置 : 主页 > 编程语言 > java >

Cookie字符串转Map集合方法

来源:互联网 收集:自由互联 发布时间:2022-07-19
需求:将Cookie字符串解析为Map集合 1、首先构造一个Cookie字符串 private static String COOKIE = "traceid=ca4c9c8b50; _ga=GA1.2.704388932.1617513691; _gid=GA1.2.85700497.1617513691; _qpsvr_localtk=0.2810983015733952; RK=uC


需求:将Cookie字符串解析为Map集合 

 1、首先构造一个Cookie字符串

private static String COOKIE = "traceid=ca4c9c8b50; _ga=GA1.2.704388932.1617513691; _gid=GA1.2.85700497.1617513691; _qpsvr_localtk=0.2810983015733952; RK=uCLoGvbeXG; ptcz=f997dfbd9d785b321bb377710933cae37308f34abb4b2340706eda53e23455cc; pgv_pvid=4861902100; pgv_info=ssid=s7567138318; ptui_loginuin=327844761; qqmusic_uin=0327844761; qqmusic_fromtag=6; qqmusic_key=@R3lRxCHCr; p_uin=o0327844761; _gat_gtag_UA_172919674_1=1; uin=o0327844761; skey=@APPCpl4ip; pt4_token=EWPb3WyZfLoQPIfmYfSjWDKOJXOIt09*xjIidJVdC54_; p_skey=6dlS-INlRzNbnEOA09*Zn2l6cfPMI4ardDbcWCwqsQI_";

2、编写Cookie字符串转map集合方法

public static Map<String,String> cookieToMap(String value) {
Map<String, String> map = new HashMap<String, String>();
value = value.replace(" ", "");
if (value.contains(";")) {
String values[] = value.split(";");
for (String val : values) {
String vals[] = val.split("=");
map.put(vals[0], vals[1]);
}
} else {
String values[] = value.split("=");
map.put(values[0], values[1]);
}
return map;
}

3、验证结果是否符合预期结果

{traceid=ca4c9c8b50, _qpsvr_localtk=0.2810983015733952, uin=o0327844761, _gid=GA1.2.85700497.1617513691, ptcz=f997dfbd9d785b321bb377710933cae37308f34abb4b2340706eda53e23455cc, _gat_gtag_UA_172919674_1=1, p_skey=6dlS-INlRzNbnEOA09*Zn2l6cfPMI4ardDbcWCwqsQI_, skey=@APPCpl4ip, pt4_token=EWPb3WyZfLoQPIfmYfSjWDKOJXOIt09*xjIidJVdC54_, pgv_info=ssid, _ga=GA1.2.704388932.1617513691, qqmusic_uin=0327844761, RK=uCLoGvbeXG, p_uin=o0327844761, qqmusic_fromtag=6, qqmusic_key=@R3lRxCHCr, ptui_loginuin=327844761, pgv_pvid=4861902100}
上一篇:Java List去除重复数据的五种方式
下一篇:没有了
网友评论