简体   繁体   English

更改请求的内容类型以处理使用application / x-www-form-urlencoded发送的XML

[英]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.

相关问题 Spring MVC中内容类型应用程序/ x-www-form-urlencoded的请求参数的顺序 - Order of request parameters for content-type application/x-www-form-urlencoded in Spring MVC 用Java处理Content-Type =“ application / x-www-form-urlencoded”的SOAP请求 - Processing SOAP request with Content-Type = “application/x-www-form-urlencoded” in Java 如何使用“ Content-type:application / x-www-form-urlencoded”发出Okhttp请求? - How to make an Okhttp Request with “Content-type:application/x-www-form-urlencoded”? ContentCachingRequestWrapper 只捕获带有 Content-Type:application/x-www-form-urlencoded 的 POST 请求 - ContentCachingRequestWrapper only captures POST request with Content-Type:application/x-www-form-urlencoded 请求正文作为 json 发送,即使内容类型设置为 application/x-www-form-urlencoded - The request body is sent as json even though the content type is set as application/x-www-form-urlencoded 如何编写控制器类以允许内容类型:application / json和application / x-www-form-urlencoded - How to write controller class to allow content-type: application/json and application/x-www-form-urlencoded 使用Jaxb2Marshaller取消编组content-type = application / x-www-form-urlencoded会引发错误“序言中不允许内容” - UnMarshalling content-type=application/x-www-form-urlencoded using Jaxb2Marshaller throws error “Content is not allowed in prolog” @JsonProperty 不适用于内容类型:application/x-www-form-urlencoded - @JsonProperty not working for Content-Type : application/x-www-form-urlencoded 在Spring Boot中使用内容类型application / x-www-form-urlencoded的请求的自定义反序列化器 - Custom deserializer for requests with content-type application/x-www-form-urlencoded in Spring Boot 当内容类型为 application/x-www-form-urlencoded 时,Java 读取 POST 数据 - Java read POST data when content-type is application/x-www-form-urlencoded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM