我有一个Play网络应用程序向服务器发出HTTP请求。
请求顺利:我收到200状态代码和Content-type =" application / xml"的回复。
如果我打印到stdout响应主体,我会看到一个结构良好的Xml doc。
但是,如果我尝试从响应中创建org.w3c.dom.XML文档 使用WSResponse.asXml(),该方法返回一个空文档。
这些是我的代码的相关部分:
private WSResponse sendPostRequest(String url, String body) {
WSRequest request = WS.url(url);
request.setHeader("Content-type", "application/x-www-form-urlencoded");
return request.post(body).get(5000L);
}
并且:
public Result requestDefaultImport() {
String url = "some_url";
String body = "some_body";
WSResponse response = sendPostRequest(url, body);
System.out.println(response.getBody()); //prints well-formed Xml
Document xmld = response.asXml();
System.out.println(xmld); //prints: #[null document]
return ok();
}
我做错了什么?
答案 0 :(得分:1)
试试这段代码:
Document doc = request().body().asXml();
Source source = new DOMSource(doc);
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
TransformerFactory factory = TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
String xmlStr = stringWriter.getBuffer().toString();
System.out.println(xmlStr);
或者如果从SOAP中消费:
Document doc = soapBody.extractContentAsDocument();
Source source = new DOMSource(doc);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
strSoapBody = stringWriter.getBuffer().toString();
答案 1 :(得分:0)
据我所知,你正在
打印一个字符串 System.out.println(response.getBody());
但是,您正在
打印org.w3c.dom.Document System.out.println(xmld);
可以在
找到将Document转换为字符串的解决方案