title: submit form using HTTPURLConnection
body: <p>Hi I am trying to submit the following form using HTTPURLConnection as a exercise. </p>
  
  <pre><code>&lt;form name="popnames" method="post" action="/cgi-bin/popularnames.cgi" onsubmit="return       submitIt();"&gt;
  &lt;p&gt;
  &lt;label for="year"&gt;Birth Year:&lt;/label&gt;&lt;br&gt;
  &lt;input type="text" name="year" size="5" maxlength="4" id="year" value="2011"&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;label for="rank"&gt;Popularity:&lt;/label&gt;&lt;br&gt;
  &lt;select name="top" size="1" id="rank"&gt;
  
  
  &lt;option value="20"&gt;Top 20&lt;/option&gt;
    &lt;option value="50"&gt;Top 50&lt;/option&gt;
    &lt;option value="100"&gt;Top 100&lt;/option&gt;
    &lt;option value="500"&gt;Top 500&lt;/option&gt;
    &lt;option value="1000"&gt;Top 1000&lt;/option&gt;
  &lt;/select&gt;
  &lt;/p&gt;
  &lt;fieldset&gt;
  &lt;legend&gt;Name rankings may include:&lt;/legend&gt;
  &lt;input type="radio" name="number" value="p" id="percent"&gt;
  &lt;label for="percent"&gt;Percent of total births&lt;/label&gt;&lt;br&gt;
  &lt;input type="radio" name="number" value="n" id="number"&gt;
  &lt;label for="number"&gt;Number of births&lt;/label&gt;
  &lt;/fieldset&gt;
  &lt;hr&gt;
  &lt;input class="uef-btn uef-btn-primary" type="submit" value="  Go  "&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I am using HTTPURLConnection to do the submit This is my code and my test class</p>
  
  <pre><code>public class FormSubmitServiceTest {
  
  @Test
  public void testSubmit() throws Exception {
      String url = "http://www.socialsecurity.gov/OACT/babynames/#ht=1";
      Map&lt;String, String&gt; data = new HashMap&lt;String, String&gt;();
      data.put("year", "2010");
      data.put("top", "50");
      data.put("number", "n");
  
      FormSubmitService service = new FormSubmitService();
      service.doSubmit(url, data);
  }
  }  
  </code></pre>
  
  <p>And my service class which does the work</p>
  
  <pre><code>public class FormSubmitService {
  
      public void doSubmit(String url, Map&lt;String, String&gt; data) throws IOException {
          URL siteUrl = new URL(url);
          HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Content-Type", 
                 "application/x-www-form-urlencoded");
          conn.setUseCaches (true);
          conn.setDoOutput(true);
          conn.setDoInput(true);
  
          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>Can anybody advise why this does not work in submitting the form. Is it because I am not clicking the submit button with a value of GO. And if that is the case then how do i actually click it because I would expect to send a name value pair across but the submit button does not have a name only a value.</p>
  
  <p>When I post the form from this code I expect the response to contain the same data as when I do the form submission manually which is the data on this page <a href="http://www.socialsecurity.gov/cgi-bin/popularnames.cgi" rel="noreferrer">http://www.socialsecurity.gov/cgi-bin/popularnames.cgi</a>.</p>
  
  <p>However on running the test class the data in the response I get is the same as the original page <a href="http://www.socialsecurity.gov/OACT/babynames/#ht=1" rel="noreferrer">http://www.socialsecurity.gov/OACT/babynames/#ht=1</a>. </p>
  
  <p>All help appreciated</p>
  
  <p>Thanks</p>