[英]Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded
Please can someone give me a hint over my question below. 请有人可以给我提示我下面的问题。 Thanks.
谢谢。
We have a XML content coming from a server in the request using content type "application/x-www-form-urlencoded". 我们在请求中使用内容类型“ application / x-www-form-urlencoded”来自服务器的XML内容。
We tried to read the request as XML in Groovy and received following error. 我们试图在Groovy中将请求读取为XML,并收到以下错误。 [Fatal Error] :1:1: Premature end of file.
[致命错误]:1:1:文件过早结束。 2015-11-16 17:15:26,777 errors.GrailsExceptionResolver SAXParseException occurred when processing request: [POST] /games/api/v1/endpoint Premature end of file.. Stacktrace follows:
2015-11-16 17:15:26,777错误。处理请求:[POST] / games / api / v1 / endpoint文件过早结束时,发生了GrailsExceptionResolver SAXParseException。
unfortunately we cannot change the content type at the server. 不幸的是,我们无法在服务器上更改内容类型。
We have tried to create a proxy to get the request from the server and change the content type to "application/xml" and then redirect to the actual endpoint so that we can read the XML properly. 我们试图创建一个代理来从服务器获取请求,并将内容类型更改为“ 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 "";
}
}
Used Apache HttpClient for the request redirection. 使用Apache HttpClient进行请求重定向。 However We still couldnt read the XML after changing the content type.
但是,更改内容类型后,我们仍然无法读取XML。
Please help to validate the solution, to know what must be going wrong, also suggest any alternatives. 请帮助验证解决方案,了解必须出问题的地方,并建议其他选择。
请尝试此操作,您需要添加一个字符集。
httppost.setHeader(HTTP.CONTENT_TYPE,"text/xml; charset=\"utf-8\"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.