当前位置: 代码迷 >> java >> 如何使用Wicket页面处理发布请求正文中的XML
  详细解决方案

如何使用Wicket页面处理发布请求正文中的XML

热度:95   发布时间:2023-07-31 10:54:48.0

我试图在设置一个页面(扩展org.apache.wicket.markup.html.WebPage),以接收来自第三方服务提供商( 正文中包含XML文档的HTTP发布请求 ,一个信用卡网关),当我访问请求输入流时,如下所示:

getWebRequest().getHttpServletRequest().getInputStream();

...该流始终为零字节。

我究竟做错了什么?

经过一些调试后,我深入了此。 创建PageParameters对象将消耗InputStream。 以下方法从PageParameters中提取xml作为字符串:

private String getXmlFromPageParameters(PageParameters params) {
// There should be a single PageParam whose key is the 
// XML document (up to the 1st =) and whose value is 
// either empty or the rest of the xml document after 
// the 1st =.
String xml = null;
for (String key : params.keySet()) {
    xml = key;
    if (params.getString(key) != null && !"".equals(params.getString(key))) {
        xml = xml + "=" + params.getString(key);
    }
}
return xml;

}

结果取决于传入流的Content-Type。 默认情况下,浏览器(例如Chrome)将内容类型设置为application / x-www-form-urlencoded或类似类型,从而导致加载PageParameters。

使用form元素上的enctype属性(例如,text / xml)覆盖默认值将导致请求的主体可通过InputStream获得(尽管它仍可能会被URLEncoded)。

使用程序源(例如Apache DefaultHttpClient)并设置Content-Type标头,还可以选择发布的正文是在PageParameters还是InputStream中。

Wicket 1.5更新

在Wicket 1.5中,PageParameters为空。 而是打电话

RequestCycle.get().getRequest().getPostParameters().getParameterValue("xml")