下面列出了java.net.HttpCookie#setDomain ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
public static List<HttpCookie> toHttpCookie(String json) {
List<HttpCookie> cookieList = new ArrayList<>();
JSONArray jsonArray = JSONArray.parseArray(json);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
HttpCookie clientCookie = new HttpCookie(jsonObject.getString("name"), jsonObject.getString("value"));
clientCookie.setDomain(jsonObject.getString("domain"));
clientCookie.setPath(jsonObject.getString("path"));
clientCookie.setMaxAge(7 * 24 * 60);
cookieList.add(clientCookie);
}
return cookieList;
}
public void testCookieWithNullPath() throws Exception {
FakeSingleCookieStore fscs = new FakeSingleCookieStore();
CookieManager cm = new CookieManager(fscs, CookiePolicy.ACCEPT_ALL);
HttpCookie cookie = new HttpCookie("foo", "bar");
cookie.setDomain("http://www.foo.com");
cookie.setVersion(0);
fscs.setNextCookie(cookie);
Map<String, List<String>> cookieHeaders = cm.get(
new URI("http://www.foo.com/log/me/in"), Collections.EMPTY_MAP);
List<String> cookies = cookieHeaders.get("Cookie");
assertEquals("foo=bar", cookies.get(0));
}
/**
* Cookie for the specified URI to save, where path and port will be verified.
*
* @param uri uri.
* @param cookieList all you want to save the Cookie, does not meet the rules will not be saved.
*/
public void add(URI uri, List<String> cookieList) {
for (String cookieValue : cookieList) {
List<HttpCookie> cookies = HttpCookie.parse(cookieValue);
for (HttpCookie cookie : cookies) {
if (cookie.getPath() == null) {
String path = normalizePath(uri.getPath());
cookie.setPath(path);
} else if (!pathMatches(uri, cookie)) {
continue;
}
if (cookie.getDomain() == null) cookie.setDomain(uri.getHost());
String portList = cookie.getPortlist();
int port = getPort(uri);
if (TextUtils.isEmpty(portList) || containsPort(portList, port)) {
cookieJar.add(uri, cookie);
}
}
}
}
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);
}
}
protected void setAuthCookieVal(String authCookie, String domain) {
this.authCookie = authCookie;
settings.edit().putString(prefKey, authCookie).apply();
/*
this is technically unnecessary if OkHttp handled the authentication, because it will
have already set the cookies in the CookieHandler. It doesn't seem to cause any issues
just to re-add the cookies, though
*/
HttpCookie httpCookie = new HttpCookie(authCookieKey, authCookie);
httpCookie.setDomain(domain);
try {
CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
cookies.add(URI.create(domain), httpCookie);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
cookieHasBeenSet = true;
android.webkit.CookieManager.getInstance().setCookie(domain, httpCookie.getName() + "=" + httpCookie.getValue());
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
android.webkit.CookieManager.getInstance().flush();
} else {
CookieSyncManager.getInstance().sync();
}
}
@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;
}
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());
}
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;
}
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());
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
/**
* Creates a well-formed cookie. The behavior when domain is unset varies between
* RFC 2965 and RFC 6265. CookieStoreImpl assumes these values are set "correctly" by the time
* it receives the HttpCookie instance.
*/
private static HttpCookie createCookie(String name, String value, String domain, String path) {
HttpCookie cookie = new HttpCookie(name, value);
cookie.setDomain(domain);
cookie.setPath(path);
return cookie;
}
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());
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
@Test public void cookiesAlwaysVersion1() throws Exception {
HttpCookie cookieVersionZero = new HttpCookie("session", "session-contents");
cookieVersionZero.setPath("/");
cookieVersionZero.setDomain("localhost");
cookieVersionZero.setVersion(0);
HttpCookie convertedCookie = JsonCookie.toHttpCookie(
JsonCookie.fromHttpCookie(cookieVersionZero));
assertThat(convertedCookie.getVersion()).isEqualTo(1);
}
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;
}
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());
}