2

所以我有以下代码:

    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>

有人可以在这里帮助我吗,将不胜感激!

4

1 回答 1

1

如果您使用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");
于 2012-10-14T03:10:03.007 回答