title: How to programmatically log into a website?
body: <p>I have looked at multiple examples on stackoverflow and cannot get it to work.</p>
  
  <p>This is the url I need to log into programmtically:<code>http://powerschool.fortschools.org/public/</code> </p>
  
  <p>I have tried many different ways but am unsuccessfully. Heres what I have currently:</p>
  
  <p>Original Url before logging in: <a href="http://powerschool.fortschools.org/public/" rel="nofollow noreferrer">http://powerschool.fortschools.org/public/</a></p>
  
  <p>Url after logging in: <a href="http://powerschool.fortschools.org/guardian/home.html" rel="nofollow noreferrer">http://powerschool.fortschools.org/guardian/home.html</a></p>
  
  <p>Form I think is submitted before login:</p>
  
  <pre><code>&lt;form action="/guardian/home.html" method="post" name="LoginForm" target="_top" id="LoginForm" onsubmit="doPCASLogin(this);"&gt;
  &lt;input type="hidden" name="pstoken" value="308732667SkW2oaVnhxqIqM5PzqdGWrXW4jdQoB8W"&gt;
  &lt;input type="hidden" name="contextData" value="2AA011214C3F506D76216C5B459574636E2269F51AC438EB11081A7C735345A8"&gt;
  &lt;input type="hidden" name="dbpw" value=""&gt;
  &lt;input type="hidden" name="translator_username" value=""&gt;
  &lt;input type="hidden" name="translator_password" value=""&gt;
  &lt;input type="hidden" name="translator_ldappassword" value=""&gt;
  
  &lt;input type="hidden" name="returnUrl" value=""&gt;
  &lt;input type="hidden" name="serviceName" value="PS Parent Portal"&gt;
  &lt;input type="hidden" name="serviceTicket" value=""&gt;
  &lt;input type="hidden" name="pcasServerUrl" value="/"&gt;
  &lt;input type="hidden" name="credentialType" value="User Id and Password Credential"&gt;
  
  &lt;h2&gt;Parent Sign In&lt;/h2&gt;
  &lt;!--box content--&gt;
  &lt;div id="noscript" class="feedback-alert" style="display: none;"&gt; To sign in to PowerSchool, you must use a browser that supports and has JavaScript enabled. &lt;/div&gt;
  &lt;fieldset id="login-inputs" class="group"&gt;
  &lt;div&gt;
    &lt;label&gt;Username&lt;/label&gt;
    &lt;input type="text" id="fieldAccount" name="account" value="" size="39"&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;label&gt;Password&lt;/label&gt;
    &lt;input type="password" name="pw" value="" size="39"&gt;&lt;div id="login-help"&gt;&lt;a href="/public/account_recovery_begin.html"&gt;Having trouble signing in?&lt;/a&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="translatorInput" style="display: none;"&gt;
    &lt;label&gt;Translator Sign In&lt;/label&gt;
    &lt;input type="password" name="translatorpw" value="" size="39"&gt;
  &lt;/div&gt;
  &lt;div class="button-row"&gt;
    &lt;button type="submit" id="btn-enter" title="Sign In To PowerSchool Parent Access" value="Enter" border="0"&gt;Sign In&lt;/button&gt;
  &lt;/div&gt;
  &lt;/fieldset&gt;       
  &lt;!-- box content--&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>After pressing the submit button the form changes to this (only the dbpw changes, (this was two seperate examples so the pstoken and the contextData did change)):
  <img src="https://i.stack.imgur.com/IiRnm.png" alt="Notice how the only thing that changes is the dbpw field"></p>
  
  <p>This is after you "fully log in"
  <img src="https://i.stack.imgur.com/lKhoK.png" alt="Data">
  <img src="https://i.stack.imgur.com/j5zX4.png" alt="respone headers">
  <img src="https://i.stack.imgur.com/dz3Ud.png" alt="cookies"></p>
  
  <p>Here is my code (not working):</p>
  
  <pre><code>public static final String POWERSCHOOLLOGINURL = "http://powerschool.fortschools.org/public/";
      public static final String POWERSCHOOLLOGIN = "http://powerschool.fortschools.org/guardian/home.html";
  
      public static void main(String[] args) throws IOException {
          Map&lt;String, String&gt; nameToVal = new HashMap&lt;String, String&gt;();
          nameToVal.put("pstoken", "308732667b2uBDHKHeNJc1XTXdgDSVwxzHfzldM9M");
          nameToVal.put("contextData", "e1c94866f2ed77f3ae37bc1a2a477631");
          nameToVal.put("dbpw", "29e3fdf45f7959a5e0c894ad01b34941");
          nameToVal.put("translator_username", "");
          nameToVal.put("translator_password", "");
          nameToVal.put("translator_ldappassword", "");
          nameToVal.put("returnUrl", "");
          nameToVal.put("serviceName", "PS Parent Portal");
          nameToVal.put("serviceTicket", "");
          nameToVal.put("pcasServerUrl", "/");
          nameToVal.put("credentialType", "User Id and Password Credential");
          nameToVal.put("account", "horvste");
          nameToVal.put("translatorpw", "");
          nameToVal.put("returnUrl", "");
          nameToVal.put("pcasServerUrl", "/");
          nameToVal.put("credentialType", "User Id and Password Credential");
          Map&lt;String, String&gt; cookies = new HashMap&lt;String, String&gt;();
          cookies.put("JSESSIONID", "2C108FB2394FFE097E366BC3C34827B8");
          cookies.put("lastHref",
                  "http%3A%2F%2Fpowerschool.fortschools.org%2Fguardian%2Fhome.html");
          cookies.put("uiStateCont", "null");
          cookies.put("uiStateNav", "null");
  
          Document doc = Jsoup.connect(POWERSCHOOLLOGIN).cookies(cookies)
                  .data(nameToVal).method(Method.POST).post();
          System.out.println(doc.toString());
  
      }
  </code></pre>
  
  <p>note: HtmlUnit is not an acceptable answer (it's to slow and doesn't work on android).</p>