1

I want to make a post request to a soap web service.

i did this:

private static HttpClient client = HttpClientBuilder.create().build();
    private static HttpHost host = new HttpHost("localhost", 8080);
HttpRequest requset = new BasicHttpRequest("POST",
                "/iOSS bla bla bla");
        requset.addHeader("Authorization", "Basic c21hcnQ2ODYzOnNtYXJ0Njg2Mw==");
        requset.addHeader("Content-Type",
                "multipart/related; type=\"text/xml\"; boundary=\"--=Part1=-- \"");
        requset.addHeader("SOAPAction", "");

I couldn't know how to add the body of the request. i didn't find the method for that

Kindly note that I am using HttpRequest to form the Http request, I am not using HttpPost to form a post request

could you help please

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • See [here](http://stackoverflow.com/questions/18419268/java-apache-hc-basichttpentityenclosingrequest-vs-basichttprequest) instead. – Sotirios Delimanolis Sep 15 '15 at 16:40
  • @SotiriosDelimanolis it uses `HttpPost` not `HttpRequest` – Marco Dinatsoli Sep 15 '15 at 16:42
  • No, look at the answer. It suggests using `HttpEntityEnclosingRequest` instead of `HttpRequest`, which otherwise doesn't provide setters for a body. – Sotirios Delimanolis Sep 15 '15 at 16:44
  • But, really, you should be using `HttpPost` (or any of the related types). – Sotirios Delimanolis Sep 15 '15 at 16:44
  • @SotiriosDelimanolis Okay I will give it a try first to use `HttpPost`, and if no success, I will try using `HttpEntityEnclosingRequest` and come back to u. Do you know please any example about HttpPost? – Marco Dinatsoli Sep 15 '15 at 16:46
  • @SotiriosDelimanolis I didn't use HttpPost because I didn't know how to instaciate an object from it – Marco Dinatsoli Sep 15 '15 at 16:47
  • [This first link](http://stackoverflow.com/questions/18188041/write-in-body-request-with-httpclient). There are various `HttpEntity` subtypes you can use (or write your own). Then just use `setEntity`. The link is a little outdated for how to create `HttpClient`. You have the more recent pattern in your sample code. – Sotirios Delimanolis Sep 15 '15 at 16:47
  • @SotiriosDelimanolis in HttpPost(url) my problem is that I didn't know if I have to put the whole URL ? or just the url that comes after the host – Marco Dinatsoli Sep 15 '15 at 16:48
  • I believe `HttpClient` allows you to do either. It provides an `execute` overload that accepts a single `HttpRequest` (which is a superinterface of `HttpPost`), where the URL should be absolute. It also provides an overload where you can provide an `HttpHost`. – Sotirios Delimanolis Sep 15 '15 at 16:50

1 Answers1

3

The HttpRequest interface does not provide a method to set the corresponding message body. Neither does BasicHttpRequest

You need to go one level deeper into the inheritance hierarchy and use HttpEntityEnclosingRequest, which exposes a setEntity(HttpEntity) method, and its default implementation BasicHttpEntityEnclosingRequest.

An HttpEntity more or less represents the http message body. The javadoc states

An entity that can be sent or received with an HTTP message.

There are a number of supported implementations, eg. StringEntity, ByteArrayEntity, FileEntity, etc. You can write your own if these aren't good enough.


Alternatively, I suggest you use the various HttpXyz classes that represent each method, eg. HttpPost. These types already come with a setEntity(HttpEntity) method when appropriate (HttpPost has one, but an HttpGet doesn't because a GET request shouldn't need a body).

The HttpClient provides execute(...) method overloads which can accept HttpRequest objects, where the URI should be absolute, and others where you can also provide an HttpHost to resolve a path.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724