【发布时间】:2014-08-21 18:13:29
【问题描述】:
我正在尝试将 XML 文件作为 Http POST 请求传递。当我在 Linux 机器上使用 CURL 测试 Web 服务并且 XML 格式正确时,该 Web 服务运行良好。我正在尝试编写一个 Java 实用程序来做同样的事情。我在 Apache Commons HttpClient 库 3.1 版中找到了一个示例,这是我的代码:
进口:
import java.io.File;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
代码:
String strURL = "https://localhost/scoring";
String strXMLFilename = "C:\\Users\\Test.xml";
File input = new File(strXMLFilename);
PostMethod post = new PostMethod(strURL);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO--");
post.setRequestEntity(entity);
Get HTTP client
HttpClient httpclient = new HttpClient();
try
{
int result = httpclient.executeMethod(post);
System.out.println("Response status code: " + result);
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());
}
finally
{
post.releaseConnection();
}
我收到一个错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpMethodBase.<clinit>(HttpMethodBase.java:104)
at Test.main(Test.java:40)
更新
添加了 Commons-logging-1.2.jar
还是报错:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:220)
at org.apache.commons.httpclient.methods.ExpectContinueMethod.<init>(ExpectContinueMethod.java:93)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.<init>(EntityEnclosingMethod.java:119)
at org.apache.commons.httpclient.methods.PostMethod.<init>(PostMethod.java:106)
at Test.main(Test.java:40)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.DecoderException
扔在这条线上:
PostMethod post = new PostMethod(strURL);
怎么了?请帮忙。
【问题讨论】:
标签: java apache-httpclient-4.x apache-commons-httpclient