Java 类java.net.HttpCookie 实例源码
项目:bottler
文件:SerializableHttpCookie.java
/**
* Create a new SerializableHttpCookie from a HttpCookie
*
* @param cookie An original cookie
* @return SerializableHttpCookie that have same contents
*/
@NotNull
public static SerializableHttpCookie of(@NotNull HttpCookie cookie) {
SerializableHttpCookie newCookie = new SerializableHttpCookie();
newCookie.name = cookie.getName();
newCookie.value = cookie.getValue();
newCookie.comment = cookie.getComment();
newCookie.commentURL = cookie.getCommentURL();
newCookie.toDiscard = cookie.getDiscard();
newCookie.domain = cookie.getDomain();
newCookie.maxAge = cookie.getMaxAge();
newCookie.path = cookie.getPath();
newCookie.portlist = cookie.getPortlist();
newCookie.secure = cookie.getSecure();
newCookie.version = cookie.getVersion();
// for Android (API level 24)
// https://developer.android.com/reference/java/net/HttpCookie.html#isHttpOnly()
try {
newCookie.httpOnly = cookie.isHttpOnly();
} catch (NoSuchMethodError e) {
newCookie.httpOnly = false;
}
return newCookie;
}
项目:GitHub
文件:CookieEntity.java
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;
}
项目:openjdk-jdk10
文件:InMemoryCookieStore.java
/**
* Remove a cookie from store
*/
public boolean remove(URI uri, HttpCookie ck) {
// argument can't be null
if (ck == null) {
throw new NullPointerException("cookie is null");
}
boolean modified = false;
lock.lock();
try {
modified = cookieJar.remove(ck);
} finally {
lock.unlock();
}
return modified;
}
项目:GitHub
文件:CookiesTest.java
@Test public void testSendingCookiesFromStore() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.start();
HttpUrl serverUrl = urlWithIpAddress(server, "/");
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookieA = new HttpCookie("a", "android");
cookieA.setDomain(serverUrl.host());
cookieA.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieA);
HttpCookie cookieB = new HttpCookie("b", "banana");
cookieB.setDomain(serverUrl.host());
cookieB.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieB);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
get(serverUrl);
RecordedRequest request = server.takeRequest();
assertEquals("a=android; b=banana", request.getHeader("Cookie"));
}
项目:hadoop-oss
文件:TestAuthenticationSessionCookie.java
@Test
public void testSessionCookie() throws IOException {
try {
startServer(true);
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}
URL base = new URL("http://" + NetUtils.getHostPortString(server
.getConnectorAddress(0)));
HttpURLConnection conn = (HttpURLConnection) new URL(base,
"/echo").openConnection();
String header = conn.getHeaderField("Set-Cookie");
List<HttpCookie> cookies = HttpCookie.parse(header);
Assert.assertTrue(!cookies.isEmpty());
Log.info(header);
Assert.assertFalse(header.contains("; Expires="));
Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
项目:hadoop-oss
文件:TestAuthenticationSessionCookie.java
@Test
public void testPersistentCookie() throws IOException {
try {
startServer(false);
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}
URL base = new URL("http://" + NetUtils.getHostPortString(server
.getConnectorAddress(0)));
HttpURLConnection conn = (HttpURLConnection) new URL(base,
"/echo").openConnection();
String header = conn.getHeaderField("Set-Cookie");
List<HttpCookie> cookies = HttpCookie.parse(header);
Assert.assertTrue(!cookies.isEmpty());
Log.info(header);
Assert.assertTrue(header.contains("; Expires="));
Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
项目:hadoop-oss
文件:TestHttpCookieFlag.java
@Test
public void testHttpsCookie() throws IOException, GeneralSecurityException {
URL base = new URL("https://" + NetUtils.getHostPortString(server
.getConnectorAddress(1)));
HttpsURLConnection conn = (HttpsURLConnection) new URL(base,
"/echo").openConnection();
conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());
String header = conn.getHeaderField("Set-Cookie");
List<HttpCookie> cookies = HttpCookie.parse(header);
Assert.assertTrue(!cookies.isEmpty());
Assert.assertTrue(header.contains("; HttpOnly"));
Assert.assertTrue(cookies.get(0).getSecure());
Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
项目:cookietray
文件:CookieTray.java
private void loadCache() {
Map<String, ?> allPairs = sharedPreferences.getAll();
for (Map.Entry<String, ?> entry : allPairs.entrySet()) {
String[] uriAndName = entry.getKey().split(COOKIE_KEY_DELIMITER_REGEXP, 2);
try {
URI uri = new URI(uriAndName[0]);
Set<HttpCookie> targetCookies = cookiesCache.get(uri);
if (targetCookies == null) {
targetCookies = new HashSet<>();
cookiesCache.put(uri, targetCookies);
}
String encoded = (String) entry.getValue();
HttpCookie cookie = new SerializableCookie(encoded).httpCookie();
targetCookies.add(cookie);
} catch (URISyntaxException e) {
Log.w(LOG_TAG, "Error while loading cookies from persistence", e);
}
}
}
项目:openjdk-jdk10
文件:InMemoryCookieStore.java
/**
* Get all cookies in cookie store, except those have expired
*/
public List<HttpCookie> getCookies() {
List<HttpCookie> rt;
lock.lock();
try {
Iterator<HttpCookie> it = cookieJar.iterator();
while (it.hasNext()) {
if (it.next().hasExpired()) {
it.remove();
}
}
} finally {
rt = Collections.unmodifiableList(cookieJar);
lock.unlock();
}
return rt;
}
项目:cookietray
文件:CookieTray.java
private List<HttpCookie> getValidCookies(URI uri) {
List<HttpCookie> validCookies = new ArrayList<>();
for (URI storedUri : cookiesCache.keySet()) {
if (uriMatch(storedUri, uri)) {
validCookies.addAll(cookiesCache.get(storedUri));
}
}
Iterator<HttpCookie> it = validCookies.iterator();
while (it.hasNext()) {
HttpCookie cookie = it.next();
if (cookie.hasExpired()) {
removeFromPersistence(uri, cookie);
cookiesCache.get(uri).remove(cookie);
it.remove();
}
}
return validCookies;
}
项目:LuaViewPlayground
文件:CookieManager.java
/**
* write net cookies
*
* @param cookiesHeader
*/
private static void updateNetResponseCookies(List<String> cookiesHeader) {
//write net cookie
final CookieStore cookieStore = getCookieManager().getCookieStore();
if (cookieStore != null && cookiesHeader != null) {
HttpCookie cookieStr = null;
for (String cookieHeader : cookiesHeader) {
if (cookieHeader != null) {
List<HttpCookie> cookies = HttpCookie.parse(cookieHeader);
if (cookies != null && cookies.size() > 0) {
cookieStr = cookies.get(0);
LogUtil.d(TAG, "write-net", cookieStr);
cookieStore.add(null, cookieStr);
}
}
}
}
}
项目:OpenJSharp
文件:InMemoryCookieStore.java
/**
* Get all cookies, which:
* 1) given uri domain-matches with, or, associated with
* given uri when added to the cookie store.
* 3) not expired.
* See RFC 2965 sec. 3.3.4 for more detail.
*/
public List<HttpCookie> get(URI uri) {
// argument can't be null
if (uri == null) {
throw new NullPointerException("uri is null");
}
List<HttpCookie> cookies = new ArrayList<HttpCookie>();
boolean secureLink = "https".equalsIgnoreCase(uri.getScheme());
lock.lock();
try {
// check domainIndex first
getInternal1(cookies, domainIndex, uri.getHost(), secureLink);
// check uriIndex then
getInternal2(cookies, uriIndex, getEffectiveURI(uri), secureLink);
} finally {
lock.unlock();
}
return cookies;
}
项目:OpenJSharp
文件:InMemoryCookieStore.java
/**
* Get all cookies in cookie store, except those have expired
*/
public List<HttpCookie> getCookies() {
List<HttpCookie> rt;
lock.lock();
try {
Iterator<HttpCookie> it = cookieJar.iterator();
while (it.hasNext()) {
if (it.next().hasExpired()) {
it.remove();
}
}
} finally {
rt = Collections.unmodifiableList(cookieJar);
lock.unlock();
}
return rt;
}
项目:OpenJSharp
文件:InMemoryCookieStore.java
/**
* Get all URIs, which are associated with at least one cookie
* of this cookie store.
*/
public List<URI> getURIs() {
List<URI> uris = new ArrayList<URI>();
lock.lock();
try {
Iterator<URI> it = uriIndex.keySet().iterator();
while (it.hasNext()) {
URI uri = it.next();
List<HttpCookie> cookies = uriIndex.get(uri);
if (cookies == null || cookies.size() == 0) {
// no cookies list or an empty list associated with
// this uri entry, delete it
it.remove();
}
}
} finally {
uris.addAll(uriIndex.keySet());
lock.unlock();
}
return uris;
}
项目:OpenJSharp
文件:InMemoryCookieStore.java
/**
* Remove a cookie from store
*/
public boolean remove(URI uri, HttpCookie ck) {
// argument can't be null
if (ck == null) {
throw new NullPointerException("cookie is null");
}
boolean modified = false;
lock.lock();
try {
modified = cookieJar.remove(ck);
} finally {
lock.unlock();
}
return modified;
}
项目:OpenJSharp
文件:InMemoryCookieStore.java
private <T> void addIndex(Map<T, List<HttpCookie>> indexStore,
T index,
HttpCookie cookie)
{
if (index != null) {
List<HttpCookie> cookies = indexStore.get(index);
if (cookies != null) {
// there may already have the same cookie, so remove it first
cookies.remove(cookie);
cookies.add(cookie);
} else {
cookies = new ArrayList<HttpCookie>();
cookies.add(cookie);
indexStore.put(index, cookies);
}
}
}
项目:minijax
文件:MinijaxNewCookieDelegate.java
@Override
public NewCookie fromString(final String value) {
if (value == null || value.isEmpty()) {
return null;
}
final List<HttpCookie> httpCookies = HttpCookie.parse(value);
final HttpCookie httpCookie = httpCookies.get(0);
return new NewCookie(
httpCookie.getName(),
httpCookie.getValue(),
httpCookie.getPath(),
httpCookie.getDomain(),
httpCookie.getVersion(),
httpCookie.getComment(),
(int) httpCookie.getMaxAge(),
null,
httpCookie.getSecure(),
httpCookie.isHttpOnly());
}
项目:openjdk-jdk10
文件:InMemoryCookieStore.java
/**
* Get all URIs, which are associated with at least one cookie
* of this cookie store.
*/
public List<URI> getURIs() {
List<URI> uris = new ArrayList<>();
lock.lock();
try {
Iterator<URI> it = uriIndex.keySet().iterator();
while (it.hasNext()) {
URI uri = it.next();
List<HttpCookie> cookies = uriIndex.get(uri);
if (cookies == null || cookies.size() == 0) {
// no cookies list or an empty list associated with
// this uri entry, delete it
it.remove();
}
}
} finally {
uris.addAll(uriIndex.keySet());
lock.unlock();
}
return uris;
}
项目:jdk8u-jdk
文件:InMemoryCookieStore.java
/**
* Get all cookies, which:
* 1) given uri domain-matches with, or, associated with
* given uri when added to the cookie store.
* 3) not expired.
* See RFC 2965 sec. 3.3.4 for more detail.
*/
public List<HttpCookie> get(URI uri) {
// argument can't be null
if (uri == null) {
throw new NullPointerException("uri is null");
}
List<HttpCookie> cookies = new ArrayList<HttpCookie>();
boolean secureLink = "https".equalsIgnoreCase(uri.getScheme());
lock.lock();
try {
// check domainIndex first
getInternal1(cookies, domainIndex, uri.getHost(), secureLink);
// check uriIndex then
getInternal2(cookies, uriIndex, getEffectiveURI(uri), secureLink);
} finally {
lock.unlock();
}
return cookies;
}
项目:jdk8u-jdk
文件:InMemoryCookieStore.java
/**
* Get all cookies in cookie store, except those have expired
*/
public List<HttpCookie> getCookies() {
List<HttpCookie> rt;
lock.lock();
try {
Iterator<HttpCookie> it = cookieJar.iterator();
while (it.hasNext()) {
if (it.next().hasExpired()) {
it.remove();
}
}
} finally {
rt = Collections.unmodifiableList(cookieJar);
lock.unlock();
}
return rt;
}
项目:jdk8u-jdk
文件:InMemoryCookieStore.java
/**
* Get all URIs, which are associated with at least one cookie
* of this cookie store.
*/
public List<URI> getURIs() {
List<URI> uris = new ArrayList<URI>();
lock.lock();
try {
Iterator<URI> it = uriIndex.keySet().iterator();
while (it.hasNext()) {
URI uri = it.next();
List<HttpCookie> cookies = uriIndex.get(uri);
if (cookies == null || cookies.size() == 0) {
// no cookies list or an empty list associated with
// this uri entry, delete it
it.remove();
}
}
} finally {
uris.addAll(uriIndex.keySet());
lock.unlock();
}
return uris;
}
项目:openjdk-jdk10
文件:InMemoryCookieStore.java
private <T> void addIndex(Map<T, List<HttpCookie>> indexStore,
T index,
HttpCookie cookie)
{
if (index != null) {
List<HttpCookie> cookies = indexStore.get(index);
if (cookies != null) {
// there may already have the same cookie, so remove it first
cookies.remove(cookie);
cookies.add(cookie);
} else {
cookies = new ArrayList<>();
cookies.add(cookie);
indexStore.put(index, cookies);
}
}
}
项目:aliyun-cupid-sdk
文件:WebSocketClient.java
private void setLoadBalanceKey(ClientUpgradeRequest request) {
if (SDKConstants.CUPID_INTERACTION_SUB_PROTOCOL_CLIENT.equals(subProtocol)
&& loadBalanceHashKey != null) {
// add hash_key cookie for load balance
int cookieVersion = 0;
String lbKey = loadBalanceHashKey;
if (loadBalanceHashKey.contains("\"")) {
cookieVersion = 1;
lbKey = lbKey.replaceAll("\"", "");
}
HttpCookie cookie = new HttpCookie(SDKConstants.CUPID_INTERACTION_COOKIE_HASH_KEY, lbKey);
cookie.setVersion(cookieVersion);
request.getCookies().add(cookie);
}
}
项目:teasy
文件:CookiesService.java
public void addHttpCookieFromResponseToHeader(final List<Header> headers, final HttpResponse httpResponse) {
String cookieValue = "";
for (Header header : httpResponse.getHeaders("Set-Cookie")) {
List<HttpCookie> httpCookies = HttpCookie.parse(header.getValue());
for (HttpCookie httpCookie : httpCookies) {
if (!cookieValue.isEmpty()) {
cookieValue += "; ";
}
cookieValue += httpCookie.getName() + "=" + httpCookie.getValue();
}
}
headers.add(new BasicHeader("Cookie", cookieValue));
}
项目:teasy
文件:CookiesService.java
private List<Cookie> addCookie(final HttpResponse httpResponse) {
String cookieValue = "";
List<Cookie> cookieList = new ArrayList<Cookie>();
for (Header header : httpResponse.getHeaders("Set-Cookie")) {
List<HttpCookie> httpCookies = HttpCookie.parse(header.getValue());
for (HttpCookie httpCookie : httpCookies) {
if (!cookieValue.isEmpty()) {
cookieValue += "; ";
}
cookieList.add(new Cookie(httpCookie.getName(), httpCookie.getValue(), httpCookie.getDomain(), httpCookie.getPath(), null, httpCookie.getSecure(), httpCookie.isHttpOnly()));
}
}
return cookieList;
}
项目:openjdk-jdk10
文件:InMemoryCookieStore.java
/**
* Add one cookie into cookie store.
*/
public void add(URI uri, HttpCookie cookie) {
// pre-condition : argument can't be null
if (cookie == null) {
throw new NullPointerException("cookie is null");
}
lock.lock();
try {
// remove the ole cookie if there has had one
cookieJar.remove(cookie);
// add new cookie if it has a non-zero max-age
if (cookie.getMaxAge() != 0) {
cookieJar.add(cookie);
// and add it to domain index
if (cookie.getDomain() != null) {
addIndex(domainIndex, cookie.getDomain(), cookie);
}
if (uri != null) {
// add it to uri index, too
addIndex(uriIndex, getEffectiveURI(uri), cookie);
}
}
} finally {
lock.unlock();
}
}
项目:GitHub
文件:CookiesTest.java
@Test
public void testNetscapeResponse() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
MockWebServer server = new MockWebServer();
server.start();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
+ "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
+ "path=/path; "
+ "domain=" + urlWithIpAddress.host() + "; "
+ "secure"));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
assertEquals("a", cookie.getName());
assertEquals("android", cookie.getValue());
assertEquals(null, cookie.getComment());
assertEquals(null, cookie.getCommentURL());
assertEquals(false, cookie.getDiscard());
assertTrue(cookie.getMaxAge() > 100000000000L);
assertEquals("/path", cookie.getPath());
assertEquals(true, cookie.getSecure());
assertEquals(0, cookie.getVersion());
}
项目:GitHub
文件:CookiesTest.java
@Test public void testRfc2109Response() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
MockWebServer server = new MockWebServer();
server.start();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
+ "Comment=this cookie is delicious; "
+ "Domain=" + urlWithIpAddress.host() + "; "
+ "Max-Age=60; "
+ "Path=/path; "
+ "Secure; "
+ "Version=1"));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
assertEquals("a", cookie.getName());
assertEquals("android", cookie.getValue());
assertEquals(null, cookie.getCommentURL());
assertEquals(false, cookie.getDiscard());
assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
assertEquals("/path", cookie.getPath());
assertEquals(true, cookie.getSecure());
}
项目:GitHub
文件:CookiesTest.java
@Test public void testQuotedAttributeValues() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
MockWebServer server = new MockWebServer();
server.start();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=\"android\"; "
+ "Comment=\"this cookie is delicious\"; "
+ "CommentURL=\"http://google.com/\"; "
+ "Discard; "
+ "Domain=" + urlWithIpAddress.host() + "; "
+ "Max-Age=60; "
+ "Path=\"/path\"; "
+ "Port=\"80,443," + server.getPort() + "\"; "
+ "Secure; "
+ "Version=\"1\""));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
assertEquals("a", cookie.getName());
assertEquals("android", cookie.getValue());
assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
assertEquals("/path", cookie.getPath());
assertEquals(true, cookie.getSecure());
}
项目:appinventor-extensions
文件:Texting.java
/**
* Processes the Set-Cookie header(s), if any, received via an
* HttpURLConnection and stores them in the cookie manager.
*
* @param conn HTTP connection over which to receive cookies
*/
void processCookies(HttpURLConnection conn) {
List<String> cookiesHeader = conn.getHeaderFields().get(COOKIES_HEADER);
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
cookies.getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
}
}
}
项目:GitHub
文件:CookiesTest.java
@Test public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
MockWebServer redirectTarget = new MockWebServer();
redirectTarget.enqueue(new MockResponse().setBody("A"));
redirectTarget.start();
HttpUrl redirectTargetUrl = urlWithIpAddress(redirectTarget, "/");
MockWebServer redirectSource = new MockWebServer();
redirectSource.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: " + redirectTargetUrl));
redirectSource.start();
HttpUrl redirectSourceUrl = urlWithIpAddress(redirectSource, "/");
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(redirectSourceUrl.host());
cookie.setPath("/");
String portList = Integer.toString(redirectSource.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(redirectSourceUrl.uri(), cookie);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
get(redirectSourceUrl);
RecordedRequest request = redirectSource.takeRequest();
assertEquals("c=cookie", request.getHeader("Cookie"));
for (String header : redirectTarget.takeRequest().getHeaders().names()) {
if (header.startsWith("Cookie")) {
fail(header);
}
}
}
项目:GitHub
文件:CallTest.java
@Test public void redirectsDoNotIncludeTooManyCookies() throws Exception {
server2.enqueue(new MockResponse().setBody("Page 2"));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: " + server2.url("/")));
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(server.getHostName());
cookie.setPath("/");
String portList = Integer.toString(server.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(server.url("/").uri(), cookie);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
Response response = client.newCall(new Request.Builder()
.url(server.url("/page1"))
.build()).execute();
assertEquals("Page 2", response.body().string());
RecordedRequest request1 = server.takeRequest();
assertEquals("c=cookie", request1.getHeader("Cookie"));
RecordedRequest request2 = server2.takeRequest();
assertNull(request2.getHeader("Cookie"));
}
项目:GitHub
文件:ResponseCacheTest.java
public void assertCookies(URL url, String... expectedCookies) throws Exception {
List<String> actualCookies = new ArrayList<>();
for (HttpCookie cookie : cookieManager.getCookieStore().get(url.toURI())) {
actualCookies.add(cookie.toString());
}
assertEquals(Arrays.asList(expectedCookies), actualCookies);
}
项目:GitHub
文件:CookiesTest.java
@Test
public void testNetscapeResponse() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
MockWebServer server = new MockWebServer();
server.start();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
+ "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
+ "path=/path; "
+ "domain=" + urlWithIpAddress.host() + "; "
+ "secure"));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
assertEquals("a", cookie.getName());
assertEquals("android", cookie.getValue());
assertEquals(null, cookie.getComment());
assertEquals(null, cookie.getCommentURL());
assertEquals(false, cookie.getDiscard());
assertTrue(cookie.getMaxAge() > 100000000000L);
assertEquals("/path", cookie.getPath());
assertEquals(true, cookie.getSecure());
assertEquals(0, cookie.getVersion());
}
项目:GitHub
文件:CookiesTest.java
@Test public void testRfc2109Response() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
MockWebServer server = new MockWebServer();
server.start();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
+ "Comment=this cookie is delicious; "
+ "Domain=" + urlWithIpAddress.host() + "; "
+ "Max-Age=60; "
+ "Path=/path; "
+ "Secure; "
+ "Version=1"));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
assertEquals("a", cookie.getName());
assertEquals("android", cookie.getValue());
assertEquals(null, cookie.getCommentURL());
assertEquals(false, cookie.getDiscard());
assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
assertEquals("/path", cookie.getPath());
assertEquals(true, cookie.getSecure());
}
项目:GitHub
文件:CookiesTest.java
@Test public void testQuotedAttributeValues() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
MockWebServer server = new MockWebServer();
server.start();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=\"android\"; "
+ "Comment=\"this cookie is delicious\"; "
+ "CommentURL=\"http://google.com/\"; "
+ "Discard; "
+ "Domain=" + urlWithIpAddress.host() + "; "
+ "Max-Age=60; "
+ "Path=\"/path\"; "
+ "Port=\"80,443," + server.getPort() + "\"; "
+ "Secure; "
+ "Version=\"1\""));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
assertEquals("a", cookie.getName());
assertEquals("android", cookie.getValue());
assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
assertEquals("/path", cookie.getPath());
assertEquals(true, cookie.getSecure());
}
项目:GitHub
文件:CookiesTest.java
@Test public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
MockWebServer redirectTarget = new MockWebServer();
redirectTarget.enqueue(new MockResponse().setBody("A"));
redirectTarget.start();
HttpUrl redirectTargetUrl = urlWithIpAddress(redirectTarget, "/");
MockWebServer redirectSource = new MockWebServer();
redirectSource.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: " + redirectTargetUrl));
redirectSource.start();
HttpUrl redirectSourceUrl = urlWithIpAddress(redirectSource, "/");
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(redirectSourceUrl.host());
cookie.setPath("/");
String portList = Integer.toString(redirectSource.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(redirectSourceUrl.uri(), cookie);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
get(redirectSourceUrl);
RecordedRequest request = redirectSource.takeRequest();
assertEquals("c=cookie", request.getHeader("Cookie"));
for (String header : redirectTarget.takeRequest().getHeaders().names()) {
if (header.startsWith("Cookie")) {
fail(header);
}
}
}
项目:GitHub
文件:CallTest.java
@Test public void redirectsDoNotIncludeTooManyCookies() throws Exception {
server2.enqueue(new MockResponse().setBody("Page 2"));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: " + server2.url("/")));
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(server.getHostName());
cookie.setPath("/");
String portList = Integer.toString(server.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(server.url("/").uri(), cookie);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
Response response = client.newCall(new Request.Builder()
.url(server.url("/page1"))
.build()).execute();
assertEquals("Page 2", response.body().string());
RecordedRequest request1 = server.takeRequest();
assertEquals("c=cookie", request1.getHeader("Cookie"));
RecordedRequest request2 = server2.takeRequest();
assertNull(request2.getHeader("Cookie"));
}
项目:Android-Client
文件:KwalaCookieStore.java
public KwalaCookieStore(Context context) {
sharedPreferences = context.getSharedPreferences(SHARED_PREFS_KEY, Context.MODE_PRIVATE);
String cookieRaw = sharedPreferences.getString(COOKIE_NAME, null);
if (cookieRaw != null) {
HttpCookie cookie = gson.fromJson(cookieRaw, HttpCookie.class);
cookieStore.add(URI.create(cookie.getDomain()), cookie);
}
}
项目:Android-Client
文件:KwalaCookieStore.java
@Override
public void add(URI uri, HttpCookie cookie) {
Log.d(TAG, "uri: " + uri.toString());
Log.d(TAG, "cookie: " + cookie.toString());
if (cookie.getName().equals(COOKIE_NAME)) {
remove(URI.create(cookie.getDomain()), cookie);
sharedPreferences.edit().putString(COOKIE_NAME, gson.toJson(cookie)).apply();
}
cookieStore.add(URI.create(cookie.getDomain()), cookie);
}