Question: I need to send a XML file using POST to a web service. I have a client application that creates a XML file which stores all the information required to send to the web application, but I am not sure how to go about sending it.

My XML looks like this:

```
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Comment>
  <Poll_ID>2</Poll_ID>
  <Name>wpoon</Name>
  <Text>asdasdas</Text>
  <Timestamp>2012-10-14T10:30:25</Timestamp>
</Comment>

```

And the RESTful service I will be sending it to has the URL:

```
http://localhost:8080/TESTINGrestful/rest/polls/comment

```

Could anyone advise me how to do this, any help would be appreciated.

Answer: 
There's a good example [here](http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/PostXML.java?view=markup) from Apache [HttpClient](http://hc.apache.org/httpclient-3.x/):

```
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://localhost:8080/TESTINGrestful/rest/polls/comment");
StringEntity input = new StringEntity("<Comment>...</Comment>");
input.setContentType("text/xml");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);

```
Similar to previous approach but with **HttpClientBuilder** rather than the deprecated DefaultHttpClient. This example also uses the contentType **Json**.

```
HttpPost postRequest = new HttpPost( "http://localhost:8080/service_url" );

StringEntity input = new StringEntity("{\"jsonExample\": \"12345\"}");
input.setContentType("application/json");
postRequest.setEntity(input);

HttpResponse response = HttpClientBuilder.create().build().execute(postRequest);

String json = EntityUtils.toString(response.getEntity());

```