java - 从 HTTP Post 接收 XML

标签 java xml apache restful-url

所以我有以下代码:

    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/

相关文章:

java - 在 Jenkins 上运行 TestFX 时如何避免 HeadlessException?

java - 记录 http 请求获取/发布参数的最佳方式(且快速)

java - Android 数据绑定(bind)将参数传递给 onClick 方法

java - 如何使用 JAXB 和 XmlJavaTypeAdapter 序列化列表?

ruby - 如何使用 nokogiri 从 xml 文件中转储模式元素?

java - Processing IDE 启动 - 未启动任何Processing 2.0 版本(测试版和稳定版)

java - JSF 2 :How include style sheets and javascript?

java - Apache Ace 可以用作 Maven 存储库吗?

Apache RewriteCond(子域)

apache - 禁用通过 Tomcat 直接访问应用程序