繁体   English   中英

HttpClient 3.1 版错误

[英]HttpClient version 3.1 error

我正在尝试将 XML 文件作为 Http POST 请求传递。 当我在 Linux 机器上使用 CURL 对其进行测试并且 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);

怎么了? 请帮忙。

您错过了类路径中的 apache commons-logging.jar 下载它并将其添加到您的类路径中。

更新:现在您需要下载并添加commons-codec.jar

您只需要将 Apache Commons Logging jar 添加到您的类路径(如果您使用的是 IDE,则是您的项目库)您可以从这里下载。

我通过导入以下依赖项解决了这个问题

    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM