title: Submit form in Java
body: <p>I need to get output of form of web page in Java, but there is a problem. My code is below.</p>
  
  <pre><code>    String query = "input=mysearch";
      String charset = "UTF-8";
      String str;
  
  
      URLConnection connection = new URL("http://www.webpage.com/").openConnection();
      connection.setDoOutput(true); // Triggers POST method.
      connection.setRequestProperty("Accept-Charset", charset);
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
      connection.getOutputStream().write(query.getBytes(charset));
      BufferedReader in = new BufferedReader(
              new InputStreamReader(connection.getInputStream(), "UTF-8")
      );
  
  
      while ((str = in.readLine()) != null) {
          System.out.println(str); 
      }
  </code></pre>
  
  <p>Problem is in that submit button on web page looks like this</p>
  
  <pre><code> &lt;a href="javascript:document.forms[0].submit();" type="submit" class="btn" id="search-btn"&gt;&lt;i class="demo-icon icon-search"&gt;&lt;/i&gt;&lt;/a&gt;
  </code></pre>
  
  <p>So it is not submitted. Instead of this is submitted different button, which belongs to different form.</p>
  
  <pre><code> &lt;input type="submit" name="btnAdmin" value="Submit" id="btnAdmin" class="button" style="width:100%;" /&gt;
  </code></pre>
  
  <p>Any suggestion how should I submit right form? </p>
  
  <p>Thanks for your help</p>