Search code examples
javaapache-httpclient-4.xapache-commons-httpclient

HttpClient version 3.1 error


I am trying to pass an XML file as a Http POST request. The webservice is working fine when I test it using CURL on a Linux box and the XML is well formed. I am trying to write a Java utility to do the same. I found a sample in Apache Commons HttpClient library version 3.1 and here is my code:

Imports:

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;

Code:

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();
}

I get an error:

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)

UPDATE

Added Commons-logging-1.2.jar

enter image description here

Still get an error:

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

Its thrown on this line:

PostMethod post = new PostMethod(strURL);

Whats wrong? Please help.


Solution

  • You miss the apache commons-logging.jar in your classpath. Download it and add it to your classpath.

    UPDATE: Now you need commons-codec.jar download and add it.