Java URLConnection在表单中发布数据并提交

时间:2013-09-11 10:22:42

标签: java jsp httpurlconnection urlconnection

我有一个Java类,我需要将用户名和密码传递给URL,并检查登录是否成功。

这是Java类:

  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();
      }

   }

这是我在网址中的表格:

<form id="javascript:void(0);" name="javascript:void(0);" onsubmit=" return fnOnSubmit();" action="javascript:void(0);" method="post">
<table class="wwFormTable">
                                                    <input type="hidden" name="userRole" value="" id="javascript:void(0);_userRole"/>                                       
                                                    <tr>
    <td class="tdLabel"><label for="javascript:void(0);_username" class="label">User Name :</label></td>
    <td
><input type="text" name="username" size="20" value="" id="javascript:void(0);_username" class="textbox" autocomplete="off"/></td>
</tr>

                                                    <tr>
    <td class="tdLabel"><label for="javascript:void(0);_password" class="label">Password :</label></td>
    <td
><input type="password" name="password" size="22" id="javascript:void(0);_password" class="textbox"/></td>
</tr>

                                                    <tr>
    <td colspan="2"><div align="right"><input type="submit" id="javascript:void(0);_label_login" name="method:execute" value="Login" class="groovybutton"/>
</div></td>
</tr>

                                                </table></form>

现在我要做的是为文本框用户名和密码分配值并提交表单。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您需要以表格

发布
username=xyz&password=abc