title: Submit a form with Java (POST)
body: <p>I'm trying to submit a form to a certain website from Java (over HTTP), but when reading the response I don't see what I expected.
  What exactly I do: first of all, I open the website in a browser, fill in the form by hand and submit it. In Chrome I can see the data that goes over the wire, i.e.:</p>
  
  <pre><code>Request URL:http://wizzair.com/en-GB/Select
  Request Method:POST
  Status Code:200 OK
  Request Headersview source
  Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en;q=0.8
  Cache-Control:max-age=0
  Connection:keep-alive
  Content-Length:1061
  Content-Type:application/x-www-form-urlencoded
  Cookie:WRUID=0; ASP.NET_SessionId=3e3ahach1d34oyhtoqfshxhe; Culture=en-GB; __utma=17431487.361991764.1292186668.1354138010.1354651562.81; __utmb=17431487.9.9.1354652614319; __utmc=17431487; __utmz=17431487.1319145359.34.18.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=wizz
  Host:wizzair.com
  Origin:http://wizzair.com
  Referer:http://wizzair.com/en-GB/Select
  User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.91 Safari/537.11
  Form Dataview URL encoded
  __EVENTTARGET:HeaderControlGroupRibbonSelectView_AvailabilitySearchInputRibbonSelectView_ButtonSubmit
  __VIEWSTATE:/wEPDwUBMGRkNSMYF94e4mXCiiJGEJbRixyidoa2QXSambTT2mm6cLs=
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$OriginStation:EIN
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$DestinationStation:OTP
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$DepartureDate:02/02/2013
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$ReturnDate:05/02/2013
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$PaxCountADT:1
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$PaxCountCHD:0
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$PaxCountINFANT:0
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$BaggageCount:0
  HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$ButtonSubmit:Search
  </code></pre>
  
  <p>So I try to simulate the same request from a Java program, i.e.:</p>
  
  <pre><code>        public void doSubmit(String url, Map&lt;String, String&gt; data) throws Exception {
      URL siteUrl = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
      conn.setRequestMethod("POST");
      conn.setDoOutput(true);
      conn.setDoInput(true);
  
      conn.setRequestProperty("Cookie", "WRUID=0; ASP.NET_SessionId=3e3ahach1d34oyhtoqfshxhe; Culture=en-GB; __utma=17431487.361991764.1292186668.1354138010.1354651562.81; __utmb=17431487.9.9.1354652614319; __utmc=17431487; __utmz=17431487.1319145359.34.18.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=wizz");
  
      DataOutputStream out = new DataOutputStream(conn.getOutputStream());
  
      Set keys = data.keySet();
      Iterator keyIter = keys.iterator();
      String content = "";
      for(int i=0; keyIter.hasNext(); i++) {
          Object key = keyIter.next();
          if(i!=0) {
              content += "&amp;";
          }
          content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
      }
      System.out.println(content);
      out.writeBytes(content);
      out.flush();
      out.close();
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line = "";
      while((line=in.readLine())!=null) {
          System.out.println(line);
      }
      in.close();
  }
  </code></pre>
  
  <p>.... which I call with the following parameters, as seen in the HTTP form data above:</p>
  
  <pre><code>    String url = "http://wizzair.com/en-GB/Select";
      Map&lt;String, String&gt; data = new TreeMap&lt;String, String&gt;();
      data.put("__EVENTTARGET", "HeaderControlGroupRibbonSelectView_AvailabilitySearchInputRibbonSelectView_ButtonSubmit");
      data.put("__VIEWSTATE", "/wEPDwUBMGRkNSMYF94e4mXCiiJGEJbRixyidoa2QXSambTT2mm6cLs=\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$OriginStation:EIN\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$DestinationStation:OTP\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$DepartureDate:02/02/2013\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$ReturnDate:05/02/2013\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$PaxCountADT:1\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$PaxCountCHD:0\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$PaxCountINFANT:0\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$BaggageCount:0\n"+
              "HeaderControlGroupRibbonSelectView$AvailabilitySearchInputRibbonSelectView$ButtonSubmit:Search"
  </code></pre>
  
  <p>However, the response I get back is simply a generic web page from that web site, not the answer I expected. What am I doing wrong ?</p>
  
  <p>Thanks a lot,
  Greetings,
      Sorin</p>