title: How can I programmatically fill out a web form with JAVA?
body: <p>I just need help filling out a web form programmatically with <a href="/questions/tagged/java" class="post-tag" title="show questions tagged &#39;java&#39;" rel="tag">java</a>. I use Apache HttpClient 4.0.1. The form looks like this:<br>
  <img src="https://i.stack.imgur.com/aFZMK.png" alt="Web Form Example"></p>
  
  <p>HTML code of it looks like this:</p>
  
  <pre><code>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" &lt;ol&gt;Some tags&lt;/ol&gt; &lt;ol&gt; 
  Do not show the ticket (pre)view when the user first comes to the "New Ticket" page.
  
  Wait until they hit preview. Ticket Box (ticket fields along with description)&lt;/ol&gt; &lt;ol&gt;form action="/tracenvir/newticket" method="post" id="propertyform"--div--input type="hidden" name="__FORM_TOKEN" value="dff95a43ddec5a653627d2c0"&lt;/ol&gt;
  
  &lt;ol&gt;input type="text" id="field-summary" name="field_summary" size="70"&lt;/ol&gt; &lt;ol&gt;textarea id="field-description" name="field_description" class="wikitext" rows="10" cols="68"&lt;/ol&gt; &lt;ol&gt;input type="hidden" name="field_status" value="new" &lt;/ol&gt; &lt;ol&gt;
  
        input type="submit" name="preview" value="Preview" &lt;/ol&gt; &lt;ol&gt;
  
        input type="submit" name="submit" value="Create ticket"&lt;/ol&gt;
  </code></pre>
  
  <p>And there are many other tags. Here's my java code:</p>
  
  <pre><code>DefaultHttpClient client = new DefaultHttpClient();
  
      client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
  
      client.setCookieStore(new BasicCookieStore());
  
  
      //**LOG IN**//
  
  
      //System.setProperty("javax.net.ssl.trustStore", "/home/rauch/NetBeansProjects/jssecacerts");
  
      HttpGet login = new HttpGet("http://localhost:8000/tracenvir/login");
  
      client.getCredentialsProvider().setCredentials(AuthScope.ANY,
  
              new UsernamePasswordCredentials("rauch", "qwerty"));
  </code></pre>
  
  <p>And then correctly Login... I get 200 OK and everything is well.</p>
  
  <pre><code>    //**POST NewTicket**
  
      HttpPost post = new HttpPost("http://localhost:8000/tracenvir/newticket");
  
      List&lt;NameValuePair&gt; formparams = new ArrayList&lt;NameValuePair&gt;();
  
          formparams.add(new BasicNameValuePair("__FORM_TOKEN", cookies.get(1).getValue()));
  
          formparams.add(new BasicNameValuePair("field_summary", "Someerror"));
  
          formparams.add(new BasicNameValuePair("field_descryption","AnyDescryption"));
  
          formparams.add(new BasicNameValuePair("field_type", "defect"));
  
          formparams.add(new BasicNameValuePair("field_priority", "major"));
  
          formparams.add(new BasicNameValuePair("field_milestone", "milestone3"));
  
          formparams.add(new BasicNameValuePair("field_component", "comp2"));
  
          formparams.add(new BasicNameValuePair("field_version", "1.0"));
  
          formparams.add(new BasicNameValuePair("field_keywords", ""));
  
          formparams.add(new BasicNameValuePair("field_cc", ""));
  
          formparams.add(new BasicNameValuePair("field_owner", "java server"));
  
          formparams.add(new BasicNameValuePair("field_status", "new"));
  
          formparams.add(new BasicNameValuePair("submit", "Create ticket"));
  
      try {
  
          UrlEncodedFormEntity entity;
  
          entity = new UrlEncodedFormEntity(formparams, "UTF-8");
  
          post.setEntity(entity);
  
          post.addHeader("Referer","http://localhost:8000/tracenvir/newticket");
  
  
          HttpResponse response = client.execute(post);
  
  
          System.out.println("Create ticket: "+response.getStatusLine());
  
  
          client.getConnectionManager().shutdown();
  
  
      } catch(UnsupportedEncodingException ex) {
  
          ex.printStackTrace();
  
      } catch(IOException ex) {
  
          ex.printStackTrace();
  
      }
  </code></pre>
  
  <p>And Server responses <strong> HTTP/1.0 200 OK </strong>. But this "New Ticket" doesn't appear at ViewTickets web page. If I do the same with a normal web browser, fill out fields and push Button "Create ticket" everything is OK and I can see this NewTicket at ViewTickets web page. This is what the browser generates request:</p>
  
  <pre><code>__FORM_TOKEN=0856803edd721d8b9592231d&amp;field_summary=fuckingStatusField&amp;field_description=mmm+status&amp;field_type=defect&amp;field_priority=major&amp;field_milestone=milestone1&amp;field_component=component1&amp;field_version=2.0&amp;field_keywords=&amp;field_cc=&amp;field_owner=ubuntu-server&amp;field_status=new&amp;submit=Create+ticket)&lt;/ol&gt;
  </code></pre>
  
  <p>Why does it not work? By default I shouldn't use this:
  <pre><code>formparams.add(new BasicNameValuePair("__FORM_TOKEN", cookies.get(1).getValue()));</p>
  
  <p></code></pre>DefaultHttpClient must do this, but it doesn't. If I comment this statement, Server responses <strong> HTTP/1.0 400 Bad Request </strong> </p>
  
  <p>What should I do to correctly fill out this form?</p>
  
  <p>I tryed to imitate Browser: Firstly GET /newticket page, And then generate POST request with Headers like Browser generates.....But programatically I have 200 OK from Server, but this NewTicket doesn't appear at list of Tickets.</p>