Java源码示例:cz.msebera.android.httpclient.impl.cookie.BasicClientCookie
示例1
/**
* Creates a new cookie with the specified name and value which applies to the specified domain,
* the specified path, and expires on the specified date.
* @param domain the domain to which this cookie applies
* @param name the cookie name
* @param value the cookie name
* @param path the path to which this cookie applies
* @param expires the date on which this cookie expires
* @param secure whether or not this cookie is secure (i.e. HTTPS vs HTTP)
* @param httpOnly whether or not this cookie should be only used for HTTP(S) headers
*/
public Cookie(final String domain, final String name, final String value, final String path, final Date expires,
final boolean secure, final boolean httpOnly) {
if (domain == null) {
throw new IllegalArgumentException("Cookie domain must be specified");
}
final BasicClientCookie cookie = new BasicClientCookie(name, value != null ? value : "");
cookie.setDomain(domain);
cookie.setPath(path);
cookie.setExpiryDate(expires);
cookie.setSecure(secure);
if (httpOnly) {
cookie.setAttribute("httponly", "true");
}
httpClientCookie_ = cookie;
}
示例2
/**
* Creates a new cookie with the specified name and value which applies to the specified domain,
* the specified path, and expires after the specified amount of time.
* @param domain the domain to which this cookie applies
* @param name the cookie name
* @param value the cookie name
* @param path the path to which this cookie applies
* @param maxAge the number of seconds for which this cookie is valid; <tt>-1</tt> indicates that the
* cookie should never expire; other negative numbers are not allowed
* @param secure whether or not this cookie is secure (i.e. HTTPS vs HTTP)
*/
public Cookie(final String domain, final String name, final String value, final String path, final int maxAge,
final boolean secure) {
final BasicClientCookie cookie = new BasicClientCookie(name, value != null ? value : "");
cookie.setDomain(domain);
cookie.setPath(path);
cookie.setSecure(secure);
if (maxAge < -1) {
throw new IllegalArgumentException("invalid max age: " + maxAge);
}
if (maxAge >= 0) {
cookie.setExpiryDate(new Date(System.currentTimeMillis() + (maxAge * 1000)));
}
httpClientCookie_ = cookie;
}
示例3
/**
* Perform a HTTP GET request with cookies which are defined in hashmap
*
* @param context
* @param url
* @param hashMap
* @param responseHandler
*/
public static void getUseCookie(Context context, String url, HashMap hashMap, AsyncHttpResponseHandler responseHandler) {
PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
if (BasicUtils.judgeNotNull(hashMap)) {
Iterator iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
Cookie cookie = new BasicClientCookie(key.toString(), value.toString());
myCookieStore.addCookie(cookie);
}
}
AsyncHttpClient client = new AsyncHttpClient();
client.setCookieStore(myCookieStore);
client.get(getAbsoluteUrl(url), responseHandler);
}
示例4
/**
* Perform a HTTP POST request with cookies which are defined in hashmap
*
* @param context
* @param url
* @param hashMap
* @param responseHandler
*/
public static void postUseCookie(Context context, String url, HashMap hashMap, AsyncHttpResponseHandler responseHandler) {
PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
if (BasicUtils.judgeNotNull(hashMap)) {
Iterator iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
Cookie cookie = new BasicClientCookie(key.toString(), value.toString());
myCookieStore.addCookie(cookie);
}
}
AsyncHttpClient client = new AsyncHttpClient();
client.setCookieStore(myCookieStore);
client.post(getAbsoluteUrl(url), responseHandler);
}
示例5
@Override
protected void initHttpClient() {
if (canCloudflare()) {
String cloudflareCookieValue = preferences.getString(getSharedKey(PREF_KEY_CLOUDFLARE_COOKIE_VALUE), null);
String cloudflareCookieDomain = getCloudflareCookieDomain();
if (cloudflareCookieValue != null && cloudflareCookieDomain != null) {
BasicClientCookie c = new BasicClientCookie(CLOUDFLARE_COOKIE_NAME, cloudflareCookieValue);
c.setDomain(cloudflareCookieDomain);
httpClient.getCookieStore().addCookie(c);
}
}
}
示例6
private void loadHanabiraCookie() {
String hanabiraCookie = preferences.getString(getSharedKey(PREF_KEY_HANABIRA_COOKIE), null);
if (hanabiraCookie != null) {
BasicClientCookie c = new BasicClientCookie(HANABIRA_COOKIE_NAME, hanabiraCookie);
c.setDomain(getDomain());
httpClient.getCookieStore().addCookie(c);
}
}
示例7
private void setKompturcodeCookie(String kompturcodeCookie) {
if (kompturcodeCookie != null && kompturcodeCookie.length() > 0) {
BasicClientCookie c = new BasicClientCookie(KOMTURCODE_COOKIE_NAME, kompturcodeCookie);
c.setDomain(CHAN_DOMAIN);
httpClient.getCookieStore().addCookie(c);
}
}
示例8
@Override
protected void initHttpClient() {
JSONObject savedCookies = new JSONObject(preferences.getString(getSharedKey(PREF_KEY_FAPTCHA_COOKIES), "{}"));
for (String board : Chan410Boards.ALL_BOARDS_SET) {
String value = savedCookies.optString(board);
if (value != null && value.length() > 0) {
BasicClientCookie c = new BasicClientCookie(board, value);
c.setDomain("." + CHAN410_DOMAIN);
c.setPath("/");
httpClient.getCookieStore().addCookie(c);
}
}
}
示例9
@Override
protected void initHttpClient() {
super.initHttpClient();
String sessionCookie = preferences.getString(getSharedKey(PREF_KEY_SESSION_COOKIE), null);
if (sessionCookie != null) {
BasicClientCookie c = new BasicClientCookie(SESSION_COOKIE_NAME, sessionCookie);
c.setDomain(MIKUBA_DOMAIN);
httpClient.getCookieStore().addCookie(c);
}
}
示例10
private void loadPhpCookies(String usingDomain) {
String phpSessionCookie = preferences.getString(getSharedKey(PREF_KEY_PHPSESSION_COOKIE), null);
if (phpSessionCookie != null) {
BasicClientCookie c = new BasicClientCookie(PHPSESSION_COOKIE_NAME, phpSessionCookie);
c.setDomain(usingDomain);
httpClient.getCookieStore().addCookie(c);
}
}
示例11
private void setCaptchaTypeCookie() {
PonyachModule thisModule = ((PonyachModule) MainApplication.getInstance().getChanModule(CHAN_NAME));
String level = thisModule.preferences.getString(thisModule.getSharedKey(PREF_KEY_CAPTCHA_LEVEL), "1");
BasicClientCookie cookie = new BasicClientCookie(CAPTCHATYPE_COOKIE_NAME, level);
cookie.setDomain(thisModule.getUsingDomain());
thisModule.httpClient.getCookieStore().addCookie(cookie);
}
示例12
/** Установить cookie к текущему клиенту */
private void setCookie(String domain, String name, String value) {
if (value == null || value.equals("")) return;
BasicClientCookie c = new BasicClientCookie(name, value);
c.setDomain(domain == null || domain.equals("") ? ("." + this.domain) : domain);
c.setPath("/");
httpClient.getCookieStore().addCookie(c);
}
示例13
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
((BasicClientCookie) cookie).setAttribute(HTTPONLY_ATTR, "true");
}