【问题标题】:HttpClient version 3.1 errorHttpClient 3.1 版错误
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多