private void validateCookie(Cookie cookie) {
   if ("cookie1".equals(cookie.getName())) {
     assertEquals("value1", cookie.getValue());
     assertEquals("/", cookie.getPath());
     assertEquals("localhost", cookie.getDomain());
     validateDate(cookie.getExpiryDate());
     assertTrue(cookie.getSecure());
   } else {
     assertEquals("cookie2", cookie.getName());
     assertEquals("value2", cookie.getValue());
     assertFalse(cookie.getSecure());
   }
 }
Exemple #2
0
  public static void login() throws Exception {

    // Dem Client den Benutzernamen und das Kennwort übergeben
    HttpClient client = new HttpClient();
    client.getParams().setParameter("vb_login_username", "Programmierklasse");
    client.getParams().setParameter("vb_login_password", "987654");

    // Text von der Forumseite abholen
    GetMethod method =
        new GetMethod(
            "http://forum.operationgamma41.de/showthread.php?1228-Erfassung-der-Moderationszeiten-!");
    try {
      client.executeMethod(method);
      Cookie[] cookies = client.getState().getCookies();
      for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        System.err.println(
            "Cookie: "
                + cookie.getName()
                + ", Value: "
                + cookie.getValue()
                + ", IsPersistent?: "
                + cookie.isPersistent()
                + ", Expiry Date: "
                + cookie.getExpiryDate()
                + ", Comment: "
                + cookie.getComment());
      }
      client.executeMethod(method);
    } catch (Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
  protected Cookie toServletCookie(org.apache.commons.httpclient.Cookie commonsCookie) {

    Cookie cookie = new Cookie(commonsCookie.getName(), commonsCookie.getValue());

    String domain = commonsCookie.getDomain();

    if (Validator.isNotNull(domain)) {
      cookie.setDomain(domain);
    }

    Date expiryDate = commonsCookie.getExpiryDate();

    if (expiryDate != null) {
      int maxAge = (int) (expiryDate.getTime() - System.currentTimeMillis());

      maxAge = maxAge / 1000;

      if (maxAge > -1) {
        cookie.setMaxAge(maxAge);
      }
    }

    String path = commonsCookie.getPath();

    if (Validator.isNotNull(path)) {
      cookie.setPath(path);
    }

    cookie.setSecure(commonsCookie.getSecure());
    cookie.setVersion(commonsCookie.getVersion());

    return cookie;
  }
  private Cookie getSessionCookie(Cookie[] cookies) {
    for (Cookie cookie : cookies) {
      if ("JSESSIONID".equals(cookie.getName())) {
        return cookie;
      }
    }

    return null;
  }
Exemple #5
0
 /**
  * 构建阶段时输出cookie值
  *
  * @param client
  */
 private static void printCookie(HttpClient client) {
   Cookie[] cookies = client.getState().getCookies();
   System.out.println("目前有" + cookies.length + "条cookie");
   int index = 0;
   for (Cookie cookie : cookies) {
     System.out.println(
         "cookie[" + index + "]:{" + cookie.getName() + "," + cookie.getValue() + "}");
     index++;
   }
 }
 private void logCookie(Cookie cookie) {
   Log_OC.d(TAG, "Cookie name: " + cookie.getName());
   Log_OC.d(TAG, "       value: " + cookie.getValue());
   Log_OC.d(TAG, "       domain: " + cookie.getDomain());
   Log_OC.d(TAG, "       path: " + cookie.getPath());
   Log_OC.d(TAG, "       version: " + cookie.getVersion());
   Log_OC.d(
       TAG,
       "       expiryDate: "
           + (cookie.getExpiryDate() != null ? cookie.getExpiryDate().toString() : "--"));
   Log_OC.d(TAG, "       comment: " + cookie.getComment());
   Log_OC.d(TAG, "       secure: " + cookie.getSecure());
 }
  /**
   * Performs RFC 2965 compliant {@link org.apache.commons.httpclient.Cookie} validation
   *
   * @param host the host from which the {@link org.apache.commons.httpclient.Cookie} was received
   * @param port the port from which the {@link org.apache.commons.httpclient.Cookie} was received
   * @param path the path from which the {@link org.apache.commons.httpclient.Cookie} was received
   * @param secure <tt>true</tt> when the {@link org.apache.commons.httpclient.Cookie} was received
   *     using a secure connection
   * @param cookie The cookie to validate
   * @throws MalformedCookieException if an exception occurs during validation
   */
  public void validate(
      final String host, int port, final String path, boolean secure, final Cookie cookie)
      throws MalformedCookieException {

    LOG.trace("enter RFC2965Spec.validate(String, int, String, " + "boolean, Cookie)");

    if (cookie instanceof Cookie2) {
      if (cookie.getName().indexOf(' ') != -1) {
        throw new MalformedCookieException("Cookie name may not contain blanks");
      }
      if (cookie.getName().startsWith("$")) {
        throw new MalformedCookieException("Cookie name may not start with $");
      }
      CookieOrigin origin = new CookieOrigin(getEffectiveHost(host), port, path, secure);
      for (Iterator i = getAttribHandlerIterator(); i.hasNext(); ) {
        CookieAttributeHandler handler = (CookieAttributeHandler) i.next();
        handler.validate(cookie, origin);
      }
    } else {
      // old-style cookies are validated according to the old rules
      this.rfc2109.validate(host, port, path, secure, cookie);
    }
  }
Exemple #8
0
  private void checkHeaders(HTTPMethod method) {
    if (debugHeaders) {
      System.out.println("\nOpenConnection Headers for " + method.getPath());
      System.out.println("Status Line: " + method.getStatusLine());
    }

    Header[] responseHeaders = method.getResponseHeaders();
    for (int i1 = 0; i1 < responseHeaders.length; i1++) {
      Header responseHeader = responseHeaders[i1];
      if (debugHeaders) System.out.print("  " + responseHeader);
      String key = responseHeader.getName();
      String value = responseHeader.getValue();

      if (key.equals("Last-Modified")) {
        lastModified = value;
        if (debugHeaders) System.out.println(" **found lastModified = " + lastModified);

      } else if (key.equals("X-Last-Extended")) {
        lastExtended = value;
        if (debugHeaders) System.out.println(" **found lastExtended = " + lastExtended);

      } else if (key.equals("X-Last-Modified-Invalid")) {
        lastModifiedInvalid = value;
        if (debugHeaders)
          System.out.println(" **found lastModifiedInvalid = " + lastModifiedInvalid);
      }
    }

    if (debugHeaders) System.out.println("OpenConnection Headers for " + method.getPath());

    Cookie[] cookies = _session.getCookies();

    if (cookies.length > 0) {
      if (debugHeaders) System.out.println("Cookies= ");

      for (int i = 0; i < cookies.length; i++) {
        Cookie cooky = cookies[i];
        if (debugHeaders) System.out.println("  " + cooky);
        if (cooky.getName().equalsIgnoreCase("jsessionid")) hasSession = true;
      }
    }
  }
 private String getHttpCookie() {
   StringBuilder strHeader = new StringBuilder();
   Cookie[] cookies = httpClient.getState().getCookies();
   for (Cookie cookie : cookies) {
     String domain = cookie.getDomain();
     String path = cookie.getPath();
     String name = cookie.getName();
     String value = cookie.getValue();
     Date expired = cookie.getExpiryDate();
     boolean isSecure = cookie.getSecure();
     strHeader.append("domain=" + domain + ";");
     strHeader.append("path=" + path + ";");
     strHeader.append(name + "=" + value + ";");
     if (expired != null) {
       strHeader.append("expired=" + expired.toGMTString() + ";");
     }
     strHeader.append("isSecure=" + isSecure + "/n");
   }
   return strHeader.toString();
 }