JsoupUtils { @Nullable public static String getCsrfToken(@Nullable final String signInPage) { if (TextUtils.isEmpty(signInPage)) { return null; } final int indexOf = signInPage.indexOf("name=\"csrf-token\""); if (indexOf == -1) { return null; } int endOfToken = -1; for (int i = indexOf; i >= 0; --i) { if (signInPage.charAt(i) == '"') { endOfToken = i; break; } } if (endOfToken < 0) { return null; } int startOfToken = -1; for (int i = endOfToken - 1; i >= 0; --i) { if (signInPage.charAt(i) == '"') { startOfToken = i + 1; break; } } if (startOfToken < 0 || endOfToken - startOfToken < 1) { return null; } else { return signInPage.substring(startOfToken, endOfToken); } }  @Nullable static String getCsrfToken(@Nullable final String signInPage); @Nullable @WorkerThread static CharSequence parse(@Nullable String text);  }
@Test public void testGetCsrfToken() throws Exception { assertNull(JsoupUtils.getCsrfToken(null)); assertNull(JsoupUtils.getCsrfToken("BLAH")); assertNull(JsoupUtils.getCsrfToken("Hello, World!")); assertTrue(CSRF_TOKEN.equals(JsoupUtils.getCsrfToken(SIGN_IN_PAGE))); }