Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过POST将XML文件发送到Java中的RESTful服务_Java_Xml_Rest - Fatal编程技术网

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

通过POST将XML文件发送到Java中的RESTful服务,java,xml,rest,Java,Xml,Rest,我需要使用POST将XML文件发送到web服务。我有一个客户端应用程序,它创建了一个XML文件,其中存储了发送到web应用程序所需的所有信息,但我不知道如何发送它 我的XML如下所示: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Comment> <Poll_ID>2</Poll_ID> <Name>wpoon</Name> <Text&g

我需要使用POST将XML文件发送到web服务。我有一个客户端应用程序,它创建了一个XML文件,其中存储了发送到web应用程序所需的所有信息,但我不知道如何发送它

我的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>
如果有人能告诉我怎么做,我们将不胜感激。

Apache有一个很好的例子:

DefaultHttpClient-httpClient=newdefaulthttpclient();
HttpPostRequest=新的HttpPost(“http://localhost:8080/TESTINGrestful/rest/polls/comment");
StringEntity输入=新StringEntity(“…”);
setContentType(“text/xml”);
setEntity(输入);
HttpResponse response=httpClient.execute(postRequest);

与前面的方法类似,但使用的是HttpClientBuilder,而不是弃用的DefaultHttpClient。此示例还使用contentTypeJson

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