0

我想将数据发布到我拥有的网站上的 html 表单中。到目前为止,我正在测试它:

HttpClient httpClient = new HttpClient();
                PostMethod postMethod = new PostMethod("website URL");
                postMethod.addParameter("company_label", customerCompany);
                postMethod.addParameter("customer_name_label", customerName);
                try {
                    httpClient.executeMethod(postMethod);
                } catch (HttpException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
                    String resp = postMethod.getResponseBodyAsString();
                } else {
                     //...postMethod.getStatusLine();
                }

但它会抛出这个错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66)
    at main.main(main.java:113)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

我是在正确的道路上做我想做的事,还是应该以不同的方式做这件事?

我尝试使用的表格看起来像

<form name ='XXXXXXX' method='post' onSubmit='return validate_b3cdee6457e94aae9dee29379f9b9470()'>
  <input type='hidden' name='done' value='XXXX' border='0'>
  <input type='hidden' name='version' value='2' border='0'>
  <table border='0' cellspacing='1' cellpadding='2' width='50%' >
    <tr>
      <td><table border='0' cellspacing='1' cellpadding='2' width='50%' >
          <tr>
            <td class='DarkText' style='color:red'><div id='XXXXXXXX' style='display:none;'></div></td>
          </tr>
        </table>
        <div id='div_webForm_b3cdee6457e94aae9dee29379f9b9470'>
          <table border='0' cellspacing='1' cellpadding='2' width='100%' >
            <tr valign='top' id='text_field_row_company' >
              <td  style=''  width='20%' id="company_label"  nowrap class='FormLabel' >Company</td>
              <td class='FormField'      ><input id="company" type='text' name='company' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_customer_name' >
              <td  style=''  width='20%' id="customer_name_label"  nowrap class='FormLabel' >Customer Name</td>
              <td class='FormField'      ><input id="customer_name" type='text' name='customer_name' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_phone' >
              <td  style=''  width='20%' id="phone_label"  nowrap class='FormLabel' >Phone</td>
              <td class='FormField'      ><input id="phone" type='text' name='phone' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_email' >
              <td  style=''  width='20%' id="email_label"  nowrap class='FormLabel' >Email</td>
              <td class='FormField'      ><input id="email" type='text' name='email' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='text_field_row_needs' >
              <td  style=''  width='20%' id="needs_label"  nowrap class='FormLabel' >Needs</td>
              <td class='FormField'      ><input id="needs" type='text' name='needs' size='30' maxlength='255' class='inputForm'  value=""       ></td>
            </tr>
            <tr valign='top' id='field_row_id_other_needs'  >
              <td    style='' width='20%' height=28px nowrap class='FormLabel'  id='field_cell_label_id_other_needs' >Other Needs</td>
              <td     class='FormField'  id='field_cell_value_id_other_needs'  ><textarea id='other_needs' name='other_needs' rows='5' cols='30'  class='inputForm'   ></textarea></td>
            </tr>
            <tr>
              <td></td>
              <td><input type='submit' name='submitButtonName' value='Submit' border='0' ></td>
            </tr>
          </table>
        </div></td>
    </tr>
  </table>
</form>
4

4 回答 4

1

是的。HTTPClient 代码正在尝试使用来自其他地方的代码......来自 Apache 日志记录的东西。您需要将该 JAR 包含到您的类路径中(在 Eclipse 中,它位于 Project Properties->Java Build Path:Libraris 选项卡中。

请参阅:获取 java.lang.ClassNotFoundException:org.apache.commons.logging.LogFactory 异常

和别的。

于 2012-10-25T20:08:56.770 回答
1

您可能会错过 lib 文件夹中相关的 jar 文件:

在这里下载: http: //commons.apache.org/logging/download_logging.cgi

于 2012-10-25T20:09:09.650 回答
1

它抱怨 Appache Commons Library。请 commons-logging.xxxxxx.jarApache commons 下载并添加到您的项目 lib/classpath 中。

于 2012-10-25T20:10:07.353 回答
1

你的代码和我一起工作正常,有 3 个 jars 和 Open JDK 1.6

commons-httpclient-3.1.jar
commons-logging/1.0.4/commons-logging-1.0.4.jar
commons-codec/1.2/commons-codec-1.2.jar

我已经测试过:

PostMethod method = new PostMethod("http://search.yahoo.com/search");

祝你好运!

于 2012-10-25T20:26:09.160 回答