0

I have downloaded the Apache Http Components 4.5 jars and I am trying to use this API for making a POST request to a URL that accepts xml as a SSTRING NOT A FILE. All of the examples I have found online require that you define a PostMethod object. This Object is supposed to be apart of the http components api, but the package for the PostMethod doesn't exist. I am positive that I have all of the jars added to my class path correctly. Is there any alternative I can use or is there anything I can do to fix this?

1 Answer 1

1

The httpcomponents project also contains a more convenient fluent API for HTTP as a separate download (fluent-hc, maven groupdId: org.apache.httpcomponents, artifactId: fluent-hc). With fluent-hc a POST with a String looks like this:

import org.apache.http.client.fluent.*;

Request post = Request.Post(url).bodyString(contentString, ContentType.APPLICATION_XML).addHeader("Accept", "application/xml");
Response resp = post.execute();
String answerContent = resp.returnContent().asString();

See https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fluent.html for more.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way to set the header to text/xml with this method? @user3653004

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.