【问题标题】:POST a form (on JMX Console) from a Java application从 Java 应用程序发布表单(在 JMX 控制台上)
【发布时间】:2018-06-27 08:16:27
【问题描述】:

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 源代码。 (从网络浏览器发布表单时,会打开一个页面,告诉我调用的操作是否成功。我希望该页面的源代码作为响应。)

【问题讨论】:

    标签: java post httpurlconnection forms


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2021-10-03
      相关资源
      最近更新 更多