本文整理汇总了Java中java.net.HttpCookie.setDomain方法的典型用法代码示例。如果您正苦于以下问题:Java HttpCookie.setDomain方法的具体用法?Java HttpCookie.setDomain怎么用?Java HttpCookie.setDomain使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.HttpCookie
的用法示例。
在下文中一共展示了HttpCookie.setDomain方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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"));
}
示例3: initCookies
import java.net.HttpCookie; //导入方法依赖的package包/类
private void initCookies() {
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
if (cookieManager.getCookieStore().getCookies().size() == 0) {
try {
String url = SharedPreferencesEditor.getInstance().getString(
Constants.PREFS_URL_SHORTCUT);
URI uri = new URI(url);
HttpCookie cookie = new HttpCookie(Constants.COOKIE_AUTH_NAME,
SharedPreferencesEditor.getInstance().getString(
Constants.PREFS_AUTH));
cookie.setDomain(uri.getHost());
cookie.setPath(Constants.COOKIE_BASE_PATH);
cookie.setVersion(0);
cookie.setMaxAge((new Date()).getTime()
+ Constants.COOKIE_LIFETIME_YEAR);
cookieManager.getCookieStore().add(uri, cookie);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
示例4: testRedirectsDoNotIncludeTooManyCookies
import java.net.HttpCookie; //导入方法依赖的package包/类
@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);
}
}
}
示例5: redirectsDoNotIncludeTooManyCookies
import java.net.HttpCookie; //导入方法依赖的package包/类
@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"));
}
示例6: readObject
import java.net.HttpCookie; //导入方法依赖的package包/类
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());
}
示例7: createHttpCookie
import java.net.HttpCookie; //导入方法依赖的package包/类
private HttpCookie createHttpCookie() {
HttpCookie cookie = new HttpCookie("name", "value");
cookie.setPath("/");
cookie.setComment("comment");
cookie.setCommentURL("localhost");
cookie.setDiscard(true);
cookie.setDomain("home");
cookie.setMaxAge(System.currentTimeMillis());
cookie.setPortlist("42");
cookie.setSecure(true);
cookie.setVersion(1);
return cookie;
}
示例8: shouldOriginUriReturnUriFromHttpCookie
import java.net.HttpCookie; //导入方法依赖的package包/类
@Test
public void shouldOriginUriReturnUriFromHttpCookie() throws URISyntaxException {
URI associated = new URI("http", "google.com", "/", "");
HttpCookie cookie = new HttpCookie("name", "value");
cookie.setDomain("abc.xyz");
OriginUri uri = new OriginUri(cookie, associated);
assertNotEquals(associated, uri.uri());
}
示例9: shouldOriginUriProperlyFormatUri
import java.net.HttpCookie; //导入方法依赖的package包/类
@Test
public void shouldOriginUriProperlyFormatUri() throws URISyntaxException {
URI associated = new URI("http", "google.com", "/", "");
HttpCookie cookie = new HttpCookie("name", "value");
cookie.setDomain("abc.xyz");
cookie.setPath("/home");
OriginUri uri = new OriginUri(cookie, associated);
assertEquals("http://abc.xyz/home", uri.uri().toString());
}
示例10: shouldSkipDotWhileFormattingUri
import java.net.HttpCookie; //导入方法依赖的package包/类
@Test
public void shouldSkipDotWhileFormattingUri() throws URISyntaxException {
URI associated = new URI("http", "google.com", "/", "");
HttpCookie cookie = new HttpCookie("name", "value");
cookie.setDomain(".abc.xyz");
OriginUri uri = new OriginUri(cookie, associated);
assertEquals("http://abc.xyz/", uri.uri().toString());
}
示例11: toHttpCookie
import java.net.HttpCookie; //导入方法依赖的package包/类
/**
* Convert to a new HttpCookie
*
* @return HttpCookie that have same contents
* @throws IllegalStateException the name is null
*/
@NotNull
public HttpCookie toHttpCookie() {
if (this.name == null) {
throw new IllegalStateException("Illegal cookie name");
}
HttpCookie cookie = new HttpCookie(this.name, this.value);
cookie.setComment(this.comment);
cookie.setCommentURL(this.commentURL);
cookie.setDiscard(this.toDiscard);
cookie.setDomain(this.domain);
cookie.setMaxAge(this.maxAge);
cookie.setPath(this.path);
cookie.setPortlist(this.portlist);
cookie.setSecure(this.secure);
cookie.setVersion(this.version);
// for Android (API level 24)
// https://developer.android.com/reference/java/net/HttpCookie.html#setHttpOnly(boolean)
try {
cookie.setHttpOnly(this.httpOnly);
} catch (NoSuchMethodError e) {
}
return cookie;
}
示例12: testOf
import java.net.HttpCookie; //导入方法依赖的package包/类
@Test
public void testOf() {
HttpCookie cookie = new HttpCookie("name", "value");
cookie.setComment("comment");
cookie.setCommentURL("commentURL");
cookie.setDiscard(true);
cookie.setDomain("example.com");
cookie.setMaxAge(12345L);
cookie.setPath("/");
cookie.setPortlist("443");
cookie.setSecure(true);
cookie.setHttpOnly(true);
cookie.setVersion(1);
SerializableHttpCookie newCookie = SerializableHttpCookie.of(cookie);
Assert.assertEquals(cookie.getName(), newCookie.getName());
Assert.assertEquals(cookie.getValue(), newCookie.getValue());
Assert.assertEquals(cookie.getComment(), newCookie.getComment());
Assert.assertEquals(cookie.getCommentURL(), newCookie.getCommentURL());
Assert.assertEquals(cookie.getDiscard(), newCookie.getDiscard());
Assert.assertEquals(cookie.getDomain(), newCookie.getDomain());
Assert.assertEquals(cookie.getMaxAge(), newCookie.getMaxAge());
Assert.assertEquals(cookie.getPath(), newCookie.getPath());
Assert.assertEquals(cookie.getPortlist(), newCookie.getPortlist());
Assert.assertEquals(cookie.getSecure(), newCookie.getSecure());
Assert.assertEquals(cookie.isHttpOnly(), newCookie.isHttpOnly());
Assert.assertEquals(cookie.getVersion(), newCookie.getVersion());
}
示例13: toString
import java.net.HttpCookie; //导入方法依赖的package包/类
@Override
public String toString(final Cookie cookie) {
final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
httpCookie.setDomain(cookie.getDomain());
httpCookie.setPath(cookie.getPath());
return httpCookie.toString();
}
示例14: checkCookieNullUri
import java.net.HttpCookie; //导入方法依赖的package包/类
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");
}