代码之家  ›  专栏  ›  技术社区  ›  tom

从Java应用程序发布表单(在JMX控制台上)

  •  0
  • tom  · 技术社区  · 8 年前

    This 我正在尝试实现,但是使用Java代码。表格如下:

    <form method="post" action="HtmlAdaptor">
      <input type="hidden" name="action" value="invokeOp">
      <input type="hidden" name="name" value="dcm4chee.archive:service=ContentEditService">
      <input type="hidden" name="methodIndex" value="29">
      <hr align='left' width='80'>
      <h4>void purgeStudy()</h4>
      <p>Purge study. (Files will be removed from FilesystemMgt service see</p>
    
      <table cellspacing="2" cellpadding="2" border="1">
        <tr class="OperationHeader">
          <th>Param</th>
          <th>ParamType</th>
          <th>ParamValue</th>
          <th>ParamDescription</th>
        </tr>
    
        <tr>
          <td>iuid</td>
          <td>java.lang.String</td>
          <td>
    
            <input type="text" name="arg0">
    
          </td>
          <td>Study Instance UID.</td>
        </tr>
    
      </table>
    
      <input type="submit" value="Invoke">
    </form>
    

    我写的程序:

    // the URL of the configuration page on the JMX Console
    URL url = new URL("http://localhost:8080/jmx-console/"
        + "HtmlAdaptor?action=inspectMBean&name="
        + "dcm4chee.archive%3Aservice%3DContentEditService");
    
    // the login information
    String userpassEnc = Base64.getEncoder().encodeToString(("admin:admin")
        .getBytes(StandardCharsets.UTF_8));
    
    // the data to be sent
    String params
        = "action=invokeOp&name=dcm4chee.archive%3Aservice%3DContentEditService"
        + "&methodIndex=29&arg0=123456&submit=Invoke";
    byte[] postData = params.getBytes(StandardCharsets.UTF_8);
    
    // Configuring the connection...
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + userpassEnc);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("charset", "utf-8");
    conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
    conn.setConnectTimeout(5000);
    conn.setDoOutput(true);
    conn.setInstanceFollowRedirects(true);
    
    // Sending the data...
    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.write(postData);
    
    // Receiving the response...
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while (in.readLine() != null) { System.out.println(in.readLine()); }
    

    结果是:

    登录成功,但由于未发布表单,因此未调用mbean方法。作为回应,我只得到网页的html源代码,它有我想发布的表单。(从Web浏览器发布表单时,将打开一个页面,告诉我调用的操作是否成功。我希望该页的源代码是一个响应。)

    1 回复  |  直到 8 年前
        1
  •  0
  •   tom    8 年前

    我发现代码中有错误,我唯一要做的就是将url更改为“ http://localhost:8080/jmx-console/HtmlAdaptor “。现在开始工作了。