本文整理汇总了Java中java.net.HttpCookie类的典型用法代码示例。如果您正苦于以下问题:Java HttpCookie类的具体用法?Java HttpCookie怎么用?Java HttpCookie使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpCookie类属于java.net包,在下文中一共展示了HttpCookie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: of
import java.net.HttpCookie; //导入依赖的package包/类
/**
* 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;
}
示例2: addHttpCookieFromResponseToHeader
import java.net.HttpCookie; //导入依赖的package包/类
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));
}
示例3: toHttpCookie
import java.net.HttpCookie; //导入依赖的package包/类
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;
}
示例4: remove
import java.net.HttpCookie; //导入依赖的package包/类
/**
* 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;
}
示例5: testSendingCookiesFromStore
import java.net.HttpCookie; //导入依赖的package包/类
@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"));
}
示例6: testSessionCookie
import java.net.HttpCookie; //导入依赖的package包/类
@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()));
}
示例7: testPersistentCookie
import java.net.HttpCookie; //导入依赖的package包/类
@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()));
}
示例8: testHttpsCookie
import java.net.HttpCookie; //导入依赖的package包/类
@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()));
}
示例9: loadCache
import java.net.HttpCookie; //导入依赖的package包/类
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);
}
}
}
示例10: getCookies
import java.net.HttpCookie; //导入依赖的package包/类
/**
* 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;
}
示例11: getValidCookies
import java.net.HttpCookie; //导入依赖的package包/类
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;
}
示例12: shouldAddCookieToSharedPreferences
import java.net.HttpCookie; //导入依赖的package包/类
@Test
public void shouldAddCookieToSharedPreferences() throws URISyntaxException {
URI uri = URI.create("http://google.com");
CookieStore store = new CookieTray(preferences);
HttpCookie httpCookie = new HttpCookie("name", "value");
store.add(uri, httpCookie);
String cookieVal = new SerializableCookie(httpCookie).asString();
verify(editor).putString(uri.toString() + "|" + httpCookie.getName(), cookieVal);
}
示例13: updateNetResponseCookies
import java.net.HttpCookie; //导入依赖的package包/类
/**
* 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);
}
}
}
}
}
示例14: get
import java.net.HttpCookie; //导入依赖的package包/类
/**
* 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;
}
示例15: getURIs
import java.net.HttpCookie; //导入依赖的package包/类
/**
* 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;
}