【发布时间】:2016-02-18 13:26:35
【问题描述】:
请有人在下面的问题上给我一个提示。谢谢。
我们在请求中有一个来自服务器的 XML 内容,使用内容类型“application/x-www-form-urlencoded”。
我们尝试在 Groovy 中将请求读取为 XML 并收到以下错误。 [致命错误] :1:1: 文件过早结束。 2015-11-16 17:15:26,777 错误。处理请求时发生 GrailsExceptionResolver SAXParseException:[POST] /games/api/v1/endpoint 文件过早结束.. Stacktrace 如下:
很遗憾,我们无法在服务器上更改内容类型。
我们尝试创建一个代理来从服务器获取请求并将内容类型更改为“application/xml”,然后重定向到实际的端点,以便我们可以正确读取 XML。
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping(value = "/e2-proxy", method = RequestMethod.POST)
String home(HttpServletRequest request) {
InputStreamEntity requestEntity = null;
try {
requestEntity = new InputStreamEntity(request.getInputStream(),ContentType.create("text/xml", Consts.UTF_8));
} catch (IOException e1) {
e1.printStackTrace();
}
HttpPost httppost = new HttpPost("URL");
httppost.setEntity(requestEntity);
requestEntity.setChunked(true);
requestEntity.setContentType("text/xml");
httppost.setHeader(HTTP.CONTENT_TYPE, "text/xml");
HttpClient client = HttpClients.createDefault();
try {
client.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
使用 Apache HttpClient 进行请求重定向。但是更改内容类型后我们仍然无法读取 XML。
请帮助验证解决方案,了解肯定出了什么问题,并提出任何替代方案。
【问题讨论】:
标签: java android xml groovy content-type