【问题标题】:Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded更改请求的 Content-Type 以处理使用 application/x-www-form-urlencoded 发送的 XML
【发布时间】: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


    【解决方案1】:

    请试试这个,你需要添加一个字符集。

    httppost.setHeader(HTTP.CONTENT_TYPE,"text/xml; charset=\"utf-8\"");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-14
      • 2019-02-04
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多