java cookie类_Java 的 Cookie 处理类详解

import java.io.IOException;

import java.net.CookieHandler;

import java.net.URI;

import java.net.URL;

import java.net.URLConnection;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Collections;

import java.util.Date;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Locale;

import java.util.Map;

public class Fetch5 {

public static void main(String args[]) throws Exception {

String urlString = "java.sun.com";

CookieHandler.setDefault(new ListCookieHandler());

URL url = new URL(urlString);

URLConnection connection = url.openConnection();

Object obj = connection.getContent();

url = new URL(urlString);

connection = url.openConnection();

obj = connection.getContent();

}

}

class ListCookieHandler extends CookieHandler {

private List cookieJar = new LinkedList();

public void put(URI uri, Map> responseHeaders) throws IOException {

List setCookieList = responseHeaders.get("Set-Cookie");

if (setCookieList != null) {

for (String item : setCookieList) {

Cookie cookie = new Cookie(uri, item);

for (Cookie existingCookie : cookieJar) {

if ((cookie.getURI().equals(existingCookie.getURI()))

&& (cookie.getName().equals(existingCookie.getName()))) {

cookieJar.remove(existingCookie);

break;

}

}

cookieJar.add(cookie);

}

}

}

public Map> get(URI uri, Map> requestHeaders)

throws IOException {

StringBuilder cookies = new StringBuilder();

for (Cookie cookie : cookieJar) {

// Remove cookies that have expired

if (cookie.hasExpired()) {

cookieJar.remove(cookie);

} else if (cookie.matches(uri)) {

if (cookies.length() > 0) {

cookies.append(", ");

}

cookies.append(cookie.toString());

}

}

Map> cookieMap = new HashMap>(requestHeaders);

if (cookies.length() > 0) {

List list = Collections.singletonList(cookies.toString());

cookieMap.put("Cookie", list);

}

System.out.println("CookieMap: " + cookieMap);

return Collections.unmodifiableMap(cookieMap);

}

}

class Cookie {

String name;

String value;

URI uri;

String domain;

Date expires;

String path;

private static DateFormat expiresFormat1 = new SimpleDateFormat("E, dd MMM yyyy k:m:s 'GMT'",

Locale.US);

private static DateFormat expiresFormat2 = new SimpleDateFormat("E, dd-MMM-yyyy k:m:s 'GMT'",

Locale.US);

public Cookie(URI uri, String header) {

String attributes[] = header.split(";");

String nameValue = attributes[0].trim();

this.uri = uri;

this.name = nameValue.substring(0, nameValue.indexOf('='));

this.value = nameValue.substring(nameValue.indexOf('=') + 1);

this.path = "/";

this.domain = uri.getHost();

for (int i = 1; i < attributes.length; i++) {

nameValue = attributes[i].trim();

int equals = nameValue.indexOf('=');

if (equals == -1) {

continue;

}

String name = nameValue.substring(0, equals);

String value = nameValue.substring(equals + 1);

if (name.equalsIgnoreCase("domain")) {

String uriDomain = uri.getHost();

if (uriDomain.equals(value)) {

this.domain = value;

} else {

if (!value.startsWith(".")) {

value = "." + value;

}

uriDomain = uriDomain.substring(uriDomain.indexOf('.'));

if (!uriDomain.equals(value)) {

throw new IllegalArgumentException("Trying to set foreign cookie");

}

this.domain = value;

}

} else if (name.equalsIgnoreCase("path")) {

this.path = value;

} else if (name.equalsIgnoreCase("expires")) {

try {

this.expires = expiresFormat1.parse(value);

} catch (ParseException e) {

try {

this.expires = expiresFormat2.parse(value);

} catch (ParseException e2) {

throw new IllegalArgumentException("Bad date format in header: " + value);

}

}

}

}

}

public boolean hasExpired() {

if (expires == null) {

return false;

}

Date now = new Date();

return now.after(expires);

}

public String getName() {

return name;

}

public URI getURI() {

return uri;

}

public boolean matches(URI uri) {

if (hasExpired()) {

return false;

}

String path = uri.getPath();

if (path == null) {

path = "/";

}

return path.startsWith(this.path);

}

public String toString() {

StringBuilder result = new StringBuilder(name);

result.append("=");

result.append(value);

return result.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值