通过POST将XML文件发送到Java的RESTful服务。

时间:2023-01-16 19:36:08

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.

我需要使用POST向web服务发送一个XML文件。我有一个客户端应用程序,它创建一个XML文件,该文件存储发送到web应用程序所需的所有信息,但我不确定如何发送它。

My XML looks like this:

我的XML是这样的:

<?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:

我要发送给rest式服务的URL是:

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

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

如果有人能给我建议,我将非常感激。

1 个解决方案

#1


12  

There's a good example here from Apache HttpClient:

Apache HttpClient有一个很好的例子:

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

#1


12  

There's a good example here from Apache HttpClient:

Apache HttpClient有一个很好的例子:

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