本文整理汇总了Java中java.net.HttpCookie.parse方法的典型用法代码示例。如果您正苦于以下问题:Java HttpCookie.parse方法的具体用法?Java HttpCookie.parse怎么用?Java HttpCookie.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.HttpCookie
的用法示例。
在下文中一共展示了HttpCookie.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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));
}
示例2: createCookies
import java.net.HttpCookie; //导入方法依赖的package包/类
private Cookie[] createCookies() {
List<String> strCookies = httpHeaders.get(HttpHeaders.COOKIE);
if (strCookies == null) {
return new Cookie[] {};
}
List<Cookie> result = new ArrayList<>();
for (String strCookie : strCookies) {
List<HttpCookie> httpCookies = HttpCookie.parse(strCookie);
for (HttpCookie httpCookie : httpCookies) {
Cookie cookie = new Cookie(httpCookie.getName(), httpCookie.getValue());
result.add(cookie);
}
}
return result.toArray(new Cookie[result.size()]);
}
示例3: 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()));
}
示例4: 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()));
}
示例5: 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()));
}
示例6: 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);
}
}
}
}
}
示例7: fromString
import java.net.HttpCookie; //导入方法依赖的package包/类
@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());
}
示例8: copyProxyCookie
import java.net.HttpCookie; //导入方法依赖的package包/类
/**
* Copy cookie from the proxy to the servlet client. Replaces cookie path to local path and renames cookie to avoid
* collisions.
*/
protected void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Header header) {
List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
String path = servletRequest.getContextPath(); // path starts with / or is empty string
path += servletRequest.getServletPath(); // servlet path starts with / or is empty string
for (HttpCookie cookie : cookies) {
// set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
String proxyCookieName = getCookieNamePrefix() + cookie.getName();
Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
servletCookie.setComment(cookie.getComment());
servletCookie.setMaxAge((int) cookie.getMaxAge());
servletCookie.setPath(path); // set to the path of the proxy servlet
// don't set cookie domain
servletCookie.setSecure(cookie.getSecure());
servletCookie.setVersion(cookie.getVersion());
servletResponse.addCookie(servletCookie);
}
}
示例9: main
import java.net.HttpCookie; //导入方法依赖的package包/类
public static void main(String... args) {
HttpCookie cookie = new HttpCookie("testCookie", "value");
cookie.setMaxAge(Integer.MIN_VALUE);
if (cookie.hasExpired()) {
throw new RuntimeException("Cookie has unexpectedly expired");
}
List<HttpCookie> cookies = HttpCookie.parse("Set-Cookie: " +
"expiredCookie=value; expires=Thu, 01 Jan 1970 00:00:00 GMT");
if (cookies.size() == 1) {
if (cookies.get(0).getMaxAge() != 0) {
throw new RuntimeException("Cookie maxAge expected to be 0");
}
} else {
throw new RuntimeException("Header was incorrectly parsed");
}
}
示例10: addCookie
import java.net.HttpCookie; //导入方法依赖的package包/类
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;
}
示例11: testHttpCookie
import java.net.HttpCookie; //导入方法依赖的package包/类
@Test
public void testHttpCookie() throws IOException {
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());
Assert.assertTrue(header.contains("; HttpOnly"));
Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
示例12: parseCookieHeaderString
import java.net.HttpCookie; //导入方法依赖的package包/类
public static Map<String, Cookie> parseCookieHeaderString(String header) {
List<HttpCookie> list = HttpCookie.parse(header);
Map<String, Cookie> map = new HashMap(list.size());
list.forEach((hc) -> {
String name = hc.getName();
Cookie c = new Cookie(name, hc.getValue());
c.putIfValueNotNull(Cookie.DOMAIN, hc.getDomain());
c.putIfValueNotNull(Cookie.PATH, hc.getPath());
c.putIfValueNotNull(Cookie.VERSION, hc.getVersion() + "");
c.putIfValueNotNull(Cookie.MAX_AGE, hc.getMaxAge() + "");
c.putIfValueNotNull(Cookie.SECURE, hc.getSecure() + "");
map.put(name, c);
});
return map;
}
示例13: fromString
import java.net.HttpCookie; //导入方法依赖的package包/类
@Override
public Cookie 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 Cookie(httpCookie.getName(), httpCookie.getValue(), httpCookie.getPath(), httpCookie.getDomain(), httpCookie.getVersion());
}
示例14: parseCookieMap
import java.net.HttpCookie; //导入方法依赖的package包/类
private static void parseCookieMap(String cookieHeader, HashMap<String,
String> cookieMap) {
List<HttpCookie> cookies = HttpCookie.parse(cookieHeader);
for (HttpCookie cookie : cookies) {
if (AuthenticatedURL.AUTH_COOKIE.equals(cookie.getName())) {
cookieMap.put(cookie.getName(), cookie.getValue());
if (cookie.getPath() != null) {
cookieMap.put("Path", cookie.getPath());
}
if (cookie.getDomain() != null) {
cookieMap.put("Domain", cookie.getDomain());
}
}
}
}
示例15: TestHttpCookie
import java.net.HttpCookie; //导入方法依赖的package包/类
TestHttpCookie(String cHeader) {
this.cHeader = cHeader;
try {
List<HttpCookie> cookies = HttpCookie.parse(cHeader);
this.cookies = cookies;
} catch (IllegalArgumentException ignored) {
cookies = null;
}
}