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()); } }
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 void doLogin(final PremiumAccount pa) throws Exception { HttpMethod method = getMethodBuilder() .setAction("https://www.4shared.com/web/login") .setAjax() .setParameter("returnTo", fileURL) .setParameter("login", pa.getUsername()) .setParameter("password", pa.getPassword()) .toPostMethod(); if (!makeRedirectedRequest(method)) { throw new ServiceConnectionProblemException("Error posting login info"); } if (getContentAsString().contains("Invalid e-mail address or password")) { throw new BadLoginException("Invalid 4Shared account login information"); } // language is based on account pref, // setting language cookie doesn't work Cookie langCookie = getCookieByName("4langcookie"); if ((langCookie == null) || !langCookie.getValue().equalsIgnoreCase("en")) { method = getMethodBuilder() .setAction( "http://www.4shared.com/web/user/language") // set account's language preference .setParameter("code", "en") .toPostMethod(); if (!makeRedirectedRequest(method)) { throw new ServiceConnectionProblemException(); } if (!getContentAsString().contains("\"status\":\"ok\"")) { throw new PluginImplementationException("Setting language to EN failed"); } } }
/** * 构建阶段时输出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 getPreload() throws Exception { final HttpMethod method = getGetMethod("http://grooveshark.com/preload.php"); if (!makeRedirectedRequest(method)) { throw new ServiceConnectionProblemException(); } final Cookie cookie = getCookieByName("PHPSESSID"); if (cookie == null) { throw new PluginImplementationException("Session cookie not found"); } sessionId = cookie.getValue(); uuid = UUID.randomUUID().toString().toUpperCase(Locale.ROOT); }
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()); }
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(); }