所以我有以下代码:
public void SendToApplication(HttpServletRequest request) throws IOException, TransformerException {
BufferedReader br = new BufferedReader(new FileReader(new File("CreatePoll.xml")));
String line;
StringBuilder sb = new StringBuilder();
while((line=br.readLine())!= null) sb.append(line.trim());
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://localhost:8080/cs9322.simple.rest.doodle/rest/polls/comment");
StringEntity input = new StringEntity(sb.toString());
input.setContentType("text/xml");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
}
读取 XML 文件 (CreatePoll.xml),
<Comment xmlns:xs="http://localhost:8080/cs9322.simple.rest.doodle/CommentSchema">
<Poll_ID>2</Poll_ID>
<Name>Bob</Name>
<Text>testing junk</Text>
<Timestamp>2012-10-14T12:37:04</Timestamp>
</Comment>
并将其发布到 Web 服务,我现在遇到的问题是在发送后尝试从 Web 服务接收 XML 响应。我要接收的 XML 是:
<comment>
<address>
</address>
</comment>
谁能帮我解决这个问题,我将不胜感激!
最佳答案
如果您使用 Apache Commons IO ,然后您可以使用 IOUtils
类从 HttpEntity
读取输入流。使用 Twitter Rest API 的示例:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://search.twitter.com/search.atom?q=elkstein&count=5");
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String body = IOUtils.toString(entity.getContent(), "UTF-8");
关于java - 从 HTTP Post 接收 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878805/