下面列出了java.net.HttpCookie#setSecure ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
clientCookie = new HttpCookie(name, value);
clientCookie.setComment((String) in.readObject());
clientCookie.setCommentURL((String) in.readObject());
clientCookie.setDomain((String) in.readObject());
clientCookie.setPath((String) in.readObject());
clientCookie.setMaxAge(in.readLong());
clientCookie.setVersion(in.readInt());
clientCookie.setSecure(in.readBoolean());
clientCookie.setDiscard(in.readBoolean());
}
public static void updateCookies(Context context) {
SimpleCookieStore cookieStore = NMBApplication.getSimpleCookieStore(context);
URL url = ACSite.getInstance().getSiteUrl();
HttpCookieWithId hcwi = cookieStore.getCookie(url, "userId");
if (hcwi != null) {
HttpCookie oldCookie = hcwi.httpCookie;
cookieStore.remove(url, oldCookie);
HttpCookie newCookie = new HttpCookie("userhash", oldCookie.getValue());
newCookie.setComment(oldCookie.getComment());
newCookie.setCommentURL(oldCookie.getCommentURL());
newCookie.setDiscard(oldCookie.getDiscard());
newCookie.setDomain(oldCookie.getDomain());
newCookie.setMaxAge(oldCookie.getMaxAge());
newCookie.setPath(oldCookie.getPath());
newCookie.setPortlist(oldCookie.getPortlist());
newCookie.setSecure(oldCookie.getSecure());
newCookie.setVersion(oldCookie.getVersion());
cookieStore.add(url, newCookie);
}
}
@Nullable
public HttpCookie to() {
if (name == null || value == null) {
return null;
}
HttpCookie cookie = new HttpCookie(name, value);
cookie.setComment(comment);
cookie.setCommentURL(commentURL);
cookie.setDiscard(discard);
cookie.setDomain(domain);
cookie.setMaxAge(maxAge);
cookie.setPath(path);
cookie.setPortlist(portList);
cookie.setSecure(secure);
cookie.setVersion(version);
return cookie;
}
public static List<HttpCookie> getCookieList(ByteBuffer buffer) {
int len = buffer.getChar();
if (len == 0) return null;
final List<HttpCookie> list = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
HttpCookie cookie = new HttpCookie(getShortString(buffer), getShortString(buffer));
cookie.setDomain(getShortString(buffer));
cookie.setPath(getShortString(buffer));
cookie.setPortlist(getShortString(buffer));
cookie.setMaxAge(buffer.getLong());
cookie.setSecure(buffer.get() == 1);
cookie.setHttpOnly(buffer.get() == 1);
list.add(cookie);
}
return list;
}
/**
* Into {@link HttpCookie}.
*
* @return {@link HttpCookie}.
*/
public HttpCookie toHttpCookie() {
HttpCookie cookie = new HttpCookie(name, value);
cookie.setComment(comment);
cookie.setCommentURL(commentURL);
cookie.setDiscard(discard);
cookie.setDomain(domain);
if (expiry == -1L)
cookie.setMaxAge(-1L);
else
cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
cookie.setPath(path);
cookie.setPortlist(portList);
cookie.setSecure(secure);
cookie.setVersion(version);
return cookie;
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
cookie = new HttpCookie(name, value);
cookie.setComment((String) in.readObject());
cookie.setCommentURL((String) in.readObject());
cookie.setDomain((String) in.readObject());
cookie.setMaxAge(in.readLong());
cookie.setPath((String) in.readObject());
cookie.setPortlist((String) in.readObject());
cookie.setVersion(in.readInt());
cookie.setSecure(in.readBoolean());
cookie.setDiscard(in.readBoolean());
setHttpOnly(in.readBoolean());
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
cookie = new HttpCookie(name, value);
cookie.setComment((String) in.readObject());
cookie.setCommentURL((String) in.readObject());
cookie.setDomain((String) in.readObject());
cookie.setMaxAge(in.readLong());
cookie.setPath((String) in.readObject());
cookie.setPortlist((String) in.readObject());
cookie.setVersion(in.readInt());
cookie.setSecure(in.readBoolean());
cookie.setDiscard(in.readBoolean());
setHttpOnly(in.readBoolean());
}
/**
* CookieStoreImpl has a strict requirement on HttpCookie.equals() to enable replacement of
* cookies with the same name.
*/
public void testCookieEquality() throws Exception {
HttpCookie baseCookie = createCookie("theme", "light", "a.com", "/");
// None of the attributes immediately below should affect equality otherwise CookieStoreImpl
// eviction will not work as intended.
HttpCookie valueCookie = createCookie("theme", "light", "a.com", "/");
valueCookie.setValue("dark");
valueCookie.setPortlist("1234");
valueCookie.setSecure(true);
valueCookie.setComment("comment");
valueCookie.setCommentURL("commentURL");
valueCookie.setDiscard(true);
valueCookie.setMaxAge(12345L);
valueCookie.setVersion(1);
assertEquals(baseCookie, valueCookie);
// Changing any of the 3 main identity attributes should render cookies unequal.
assertNotEquals(createCookie("theme2", "light", "a.com", "/"), baseCookie);
assertNotEquals(createCookie("theme", "light", "b.com", "/"), baseCookie);
assertNotEquals(createCookie("theme", "light", "a.com", "/path"), baseCookie);
}
private void createCookie(Cookies cookies, String name, String token) {
HttpCookie jwtCookie = new HttpCookie(name, token);
jwtCookie.setSecure(true);
jwtCookie.setHttpOnly(true);
jwtCookie.setVersion(0);
cookies.set(jwtCookie);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
clientCookie = new HttpCookie(name, value);
clientCookie.setComment((String) in.readObject());
clientCookie.setCommentURL((String) in.readObject());
clientCookie.setDomain((String) in.readObject());
clientCookie.setMaxAge(in.readLong());
clientCookie.setPath((String) in.readObject());
clientCookie.setPortlist((String) in.readObject());
clientCookie.setVersion(in.readInt());
clientCookie.setSecure(in.readBoolean());
clientCookie.setDiscard(in.readBoolean());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
cookie = new HttpCookie(name, value);
cookie.setComment((String) in.readObject());
cookie.setCommentURL((String) in.readObject());
cookie.setDomain((String) in.readObject());
cookie.setMaxAge(in.readLong());
cookie.setPath((String) in.readObject());
cookie.setPortlist((String) in.readObject());
cookie.setVersion(in.readInt());
cookie.setSecure(in.readBoolean());
cookie.setDiscard(in.readBoolean());
// setHttpOnly(in.readBoolean());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
String name = (String) in.readObject();
String value = (String) in.readObject();
clientCookie = new HttpCookie(name, value);
clientCookie.setComment((String) in.readObject());
clientCookie.setCommentURL((String) in.readObject());
clientCookie.setDomain((String) in.readObject());
clientCookie.setMaxAge(in.readLong());
clientCookie.setPath((String) in.readObject());
clientCookie.setPortlist((String) in.readObject());
clientCookie.setVersion(in.readInt());
clientCookie.setSecure(in.readBoolean());
clientCookie.setDiscard(in.readBoolean());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
clientCookie = new HttpCookie(name, value);
clientCookie.setComment((String) in.readObject());
clientCookie.setCommentURL((String) in.readObject());
clientCookie.setDomain((String) in.readObject());
clientCookie.setMaxAge(in.readLong());
clientCookie.setPath((String) in.readObject());
clientCookie.setPortlist((String) in.readObject());
clientCookie.setVersion(in.readInt());
clientCookie.setSecure(in.readBoolean());
clientCookie.setDiscard(in.readBoolean());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
cookie = new HttpCookie(name, value);
cookie.setComment((String) in.readObject());
cookie.setCommentURL((String) in.readObject());
cookie.setDomain((String) in.readObject());
cookie.setMaxAge(in.readLong());
cookie.setPath((String) in.readObject());
cookie.setPortlist((String) in.readObject());
cookie.setVersion(in.readInt());
cookie.setSecure(in.readBoolean());
cookie.setDiscard(in.readBoolean());
setHttpOnly(in.readBoolean());
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
String name = (String) in.readObject();
String value = (String) in.readObject();
mClientCookie = new HttpCookie(name, value);
mClientCookie.setComment((String) in.readObject());
mClientCookie.setCommentURL((String) in.readObject());
mClientCookie.setDomain((String) in.readObject());
mClientCookie.setMaxAge(in.readLong());
mClientCookie.setPath((String) in.readObject());
mClientCookie.setPortlist((String) in.readObject());
mClientCookie.setVersion(in.readInt());
mClientCookie.setSecure(in.readBoolean());
mClientCookie.setDiscard(in.readBoolean());
}
private HttpCookie cookie2HttpCookie(Cookie cookie) {
HttpCookie httpCookie = new HttpCookie(cookie.name(), cookie.value());
if (cookie.expiresAt() < System.currentTimeMillis()) {
httpCookie.setMaxAge(-100L);
} else {
httpCookie.setMaxAge((cookie.expiresAt() - System.currentTimeMillis()) / 1000);
}
httpCookie.setDomain(cookie.domain());
httpCookie.setPath(cookie.path());
httpCookie.setSecure(cookie.secure());
return httpCookie;
}
public static HttpCookie toHttpCookie(JsonCookie cookieContents) {
HttpCookie cookie = new HttpCookie(cookieContents.name(), cookieContents.value());
cookie.setDomain(cookieContents.domain());
cookie.setPath(cookieContents.path());
cookie.setSecure(cookieContents.isSecure());
cookie.setHttpOnly(cookieContents.isHttpOnly());
cookie.setVersion(1); // Always set version to 1 or important fields will be dropped
return cookie;
}
public void testCookieStoreGet() throws Exception {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie cookiePort1 = createCookie("a1", "android", "a.com", "/path1");
HttpCookie cookiePort2 = createCookie("a2", "android", "a.com", "/path2");
HttpCookie secureCookie = createCookie("a3", "android", "a.com", "/path3");
secureCookie.setSecure(true);
HttpCookie notSecureCookie = createCookie("a4", "android", "a.com", "/path4");
HttpCookie bCookie = createCookie("b1", "android", "b.com", "/path5");
cookieStore.add(new URI("http://a.com:443/path1"), cookiePort1);
cookieStore.add(new URI("http://a.com:8080/path2"), cookiePort2);
cookieStore.add(new URI("https://a.com:443/path3"), secureCookie);
cookieStore.add(new URI("https://a.com:443/path4"), notSecureCookie);
cookieStore.add(new URI("https://b.com:8080/path5"), bCookie);
List<HttpCookie> expectedStoreCookies = new ArrayList<>();
expectedStoreCookies.add(cookiePort1);
expectedStoreCookies.add(cookiePort2);
expectedStoreCookies.add(secureCookie);
expectedStoreCookies.add(notSecureCookie);
// The default CookieStore implementation on Android is currently responsible for matching
// the host/domain but not handling other cookie rules: it ignores the scheme (e.g. "secure"
// checks), port and path.
// The tests below fail on the RI. It looks like in the RI it is CookieStoreImpl that is
// enforcing "secure" checks.
assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:443/anypath")));
assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:8080/anypath")));
assertEquals(expectedStoreCookies, cookieStore.get(new URI("https://a.com/anypath")));
assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com/anypath")));
}
public void testCookieStoreGetWithSecure() throws Exception {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie cookie = createCookie("theme", "light", "a.com", "/path");
cookie.setSecure(true);
cookieStore.add(new URI("https://a.com/path"), cookie);
// CookieStoreImpl on Android ignores the "Secure" attribute. The RI implements the secure
// check in CookieStoreImpl. For safety / app compatibility, if this is changed Android
// should probably implement it in both places.
assertEquals(1, cookieStore.get(new URI("http://a.com/path")).size());
assertEquals(1, cookieStore.get(new URI("https://a.com/path")).size());
}