title: Submitting an html-form with java
body: <p>I'm trying to create a login app for a campus network. I've gotten pretty far but now I'm stuck.</p>
  
  <p>I'm at a point where java returns me a page that contains a simple form that should auto submit:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;   
  &lt;body onload="document.forms[0].submit()"&gt;       
  &lt;noscript&gt;     
      &lt;p&gt;      
          &lt;strong&gt;Note:&lt;/strong&gt; Since your browser does not support JavaScript,                you must press the Continue button once to proceed.            
      &lt;/p&gt;        
  &lt;/noscript&gt;                
  
  &lt;form action="url" method="post"&gt;
      &lt;div&gt; 
          &lt;input type="hidden" name="RelayState" value="ss:mem:43141e4108638211a81427d145c7e9de"/&gt;        
  
          &lt;input type="hidden" name="SAMLResponse" value="base64string"/&gt;         
      &lt;/div&gt;    
      &lt;noscript&gt;          
          &lt;div&gt;    
              &lt;input type="submit" value="Continue"/&gt;      
          &lt;/div&gt;     
      &lt;/noscript&gt;     
  &lt;/form&gt;     
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>When I copy this string to an .html file and click on submit, it logs me in just fine. Now i'm trying to do this in java, so I extract RelayState and SAMLResponse and use the following code to submit:</p>
  
  <pre><code>HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  
  String params = "RelayState="+relayState+"&amp;SAMLResponse="+saml;
  System.out.println(params.length());
  
  urlConnection.setRequestMethod("POST");
  urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  urlConnection.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));
  urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20100101 Firefox/10.0.2");
  
  
  urlConnection.setDoInput(true);
  urlConnection.setDoOutput(true);
  
  DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
  wr.writeBytes(params);
  wr.flush();
  wr.close();
  
  InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  
  DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
  
  String fullPage = "";
  String s;
  while ((s = dis.readLine()) != null)
  {
      fullPage += s;
  }
  
  urlConnection.disconnect();
  </code></pre>
  
  <p>This code returns a 500 - internal server error. What am I missing?</p>
  
  <p>To test what was being sent, I changed the url to a simple print_r($_POST) and print_r(getAllHeaders()) and this is what I got:</p>
  
  <pre><code>//when I submit using java:
  Array(    
      [Accept] =&gt; text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2    
      [Connection] =&gt; keep-alive    
      [Content-Length] =&gt; 14176    
      [Content-Type] =&gt; application/x-www-form-urlencoded    
      [Host] =&gt; xxx   
      [User-Agent] =&gt; Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
  )
  
  Array(    
      [RelayState] =&gt; ss:mem:43141e4108638211a81427d145c7e9de
      [SAMLResponse] =&gt; base64string
  )
  
  
  
  //when I submit by copying the string to my browser and clicking submit
  Array
  (
      [Accept] =&gt; text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      [Accept-Encoding] =&gt; gzip, deflate
      [Accept-Language] =&gt; en-us,en;q=0.5
      [Connection] =&gt; keep-alive
      [Content-Length] =&gt; 14204
      [Content-Type] =&gt; application/x-www-form-urlencoded
      [DNT] =&gt; 1
      [Host] =&gt; xxx
      [User-Agent] =&gt; Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
  )
  Array
  (
      [RelayState] =&gt; ss:mem:43141e4108638211a81427d145c7e9de
      [SAMLResponse] =&gt; base64string
  )
  </code></pre>
  
  <p>I notice that the content-length header isn't the same for the two cases, but I don't know why that is or if that has anything to do with it.</p>