private void clearAndAddPersistentCookies() {
   List<Cookie> cookies = new ArrayList<Cookie>(store.getCookies());
   store.clear();
   for (Cookie cookie : cookies) {
     if (cookie.isPersistent()) {
       store.addCookie(cookie);
     }
   }
 }
  @Override
  public void addCookie(Cookie cookie) {
    if (omitNonPersistentCookies && !cookie.isPersistent()) return;
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
      cookies.put(name, cookie);
    } else {
      cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
  }
 public boolean isPersistent() {
   return cookie.isPersistent();
 }
示例#4
0
  public boolean login() throws HTTPException {
    LoginParameters loginParameters = new LoginParameters();

    // Check the client cookies.  If 'enwikiUserID', 'enwikiUserName', and 'centralauth_Token' are
    // valid, already logged in.
    List<Cookie> cookies = client.getCookieStore().getCookies();
    int numCookieFound = 0;
    Iterator<Cookie> cookieItr = cookies.iterator();
    while (cookieItr.hasNext()) {
      Cookie cookie = cookieItr.next();
      if (cookie.getName().equalsIgnoreCase("enwikiUserID")) numCookieFound++;
      else if (cookie.getName().equalsIgnoreCase("enwikiUsername")) numCookieFound++;
      else if (cookie.getName().equalsIgnoreCase("centralauth_Token")) numCookieFound++;
      if (numCookieFound == 3) return true;
    }

    // Send the initial login request
    String loginQuery =
        url + "api.php?action=login&lgname=" + username + "&lgpassword="******"&format=xml";
    HttpPost loginRequest = new HttpPost(loginQuery);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody;
    try {
      responseBody = client.execute(loginRequest, responseHandler);
    } catch (ClientProtocolException e) {
      throw new HTTPException("An HTTP protocol error occurred.");
    } catch (IOException e) {
      e.printStackTrace();
      throw new HTTPException("The connection was aborted.");
    }
    loginParameters.setXMLParameters(responseBody);

    // Send the confirm token request
    String confirmTokenQuery =
        url
            + "api.php?action=login&lgname="
            + username
            + "&lgpassword="******"&lgtoken="
            + loginParameters.getToken()
            + "&format=xml";
    HttpPost confirmTokenRequest = new HttpPost(confirmTokenQuery);
    try {
      responseBody = client.execute(confirmTokenRequest, responseHandler);
    } catch (ClientProtocolException e) {
      throw new HTTPException("An HTTP protocol error occurred.");
    } catch (IOException e) {
      throw new HTTPException("The connection was aborted.");
    }
    loginParameters.setXMLParameters(responseBody);

    // Save the cookie information.
    cookies = client.getCookieStore().getCookies();
    cookieItr = cookies.iterator();
    ArrayList<String> cookieInfo = new ArrayList<String>();
    while (cookieItr.hasNext()) {
      Cookie cookie = cookieItr.next();
      if (cookie.isPersistent() && !cookie.isExpired(new Date())) {
        String cookieDetails =
            cookie.getComment()
                + ","
                + cookie.getCommentURL()
                + ","
                + cookie.getDomain()
                + ","
                + cookie.getName()
                + ","
                + cookie.getPath()
                + ","
                + cookie.getValue()
                + ","
                + cookie.getVersion()
                + ","
                + cookie.getExpiryDate().toString();
        cookieInfo.add(cookieDetails);
      }
    }
    addCookiesToFile(cookieInfo);

    return false;
  }