title: Java URLConnection Post data in form and submit
body: <p>I have a Java class from which I need to pass username and password to an URL and check whether the login is succeeded or not. </p>
  
  <p>Here is the Java Class:</p>
  
  <pre><code>  public void testIt(String username, String password){
  
        String https_url = "http://www.myurlgoeshere.com";
        URL url;
        try {
  
           url = new URL(https_url);
           HttpURLConnection con = (HttpURLConnection)url.openConnection();
           con.setDoOutput(true);
           con.setDoInput(true);
           OutputStreamWriter oStream = new OutputStreamWriter(con.getOutputStream());
           oStream.write("admin");
           InputStream in = con.getInputStream();
  
                  System.out.println(in);
           //dumpl all cert info
           print_https_cert(con);
  
           //dump all the content
           print_content(con);
  
        } catch (MalformedURLException e) {
           e.printStackTrace();
        } catch (IOException e) {
           e.printStackTrace();
        }
  
     }
  </code></pre>
  
  <p>This is the form i have in the web URL:</p>
  
  <pre><code>&lt;form id="javascript:void(0);" name="javascript:void(0);" onsubmit=" return fnOnSubmit();" action="javascript:void(0);" method="post"&gt;
  &lt;table class="wwFormTable"&gt;
                                                      &lt;input type="hidden" name="userRole" value="" id="javascript:void(0);_userRole"/&gt;                                       
                                                      &lt;tr&gt;
      &lt;td class="tdLabel"&gt;&lt;label for="javascript:void(0);_username" class="label"&gt;User Name :&lt;/label&gt;&lt;/td&gt;
      &lt;td
  &gt;&lt;input type="text" name="username" size="20" value="" id="javascript:void(0);_username" class="textbox" autocomplete="off"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
                                                      &lt;tr&gt;
      &lt;td class="tdLabel"&gt;&lt;label for="javascript:void(0);_password" class="label"&gt;Password :&lt;/label&gt;&lt;/td&gt;
      &lt;td
  &gt;&lt;input type="password" name="password" size="22" id="javascript:void(0);_password" class="textbox"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
                                                      &lt;tr&gt;
      &lt;td colspan="2"&gt;&lt;div align="right"&gt;&lt;input type="submit" id="javascript:void(0);_label_login" name="method:execute" value="Login" class="groovybutton"/&gt;
  &lt;/div&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
                                                  &lt;/table&gt;&lt;/form&gt;
  </code></pre>
  
  <p>Now what i want to do is assign values to the textboxes  username and password and submit the form. Can anyone help?</p>