コード例 #1
0
  @Override
  public void writeExternal(ObjectOutput output) throws IOException {
    String data;
    Date date;
    List<Cookie> cookie_list = getCookies();
    output.writeInt(cookie_list.size());
    for (Cookie cookie : cookie_list) {
      output.writeUTF(cookie.getName());
      output.writeUTF(cookie.getValue());

      data = cookie.getComment();
      output.writeBoolean(data != null);
      if (data != null) output.writeUTF(data);

      date = cookie.getExpiryDate();
      output.writeBoolean(date != null);
      if (date != null) output.writeLong(date.getTime());

      data = cookie.getDomain();
      output.writeBoolean(data != null);
      if (data != null) output.writeUTF(data);

      data = cookie.getPath();
      output.writeBoolean(data != null);
      if (data != null) output.writeUTF(data);

      output.writeBoolean(cookie.isSecure());
      output.writeInt(cookie.getVersion());
    }
  }
コード例 #2
0
 private void writeObject(ObjectOutputStream out) throws IOException {
   out.writeObject(cookie.getName());
   out.writeObject(cookie.getValue());
   out.writeObject(cookie.getComment());
   out.writeObject(cookie.getDomain());
   out.writeObject(cookie.getExpiryDate());
   out.writeObject(cookie.getPath());
   out.writeInt(cookie.getVersion());
   out.writeBoolean(cookie.isSecure());
 }
コード例 #3
0
 public String getComment() {
   return cookie.getComment();
 }
コード例 #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;
  }