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

发布于 2024-10-28 04:48:54 字数 406 浏览 1 评论 0 原文

我尝试在 Wicket 中设置一个页面(扩展 org.apache.wicket.markup. html.WebPage) 以接收来自第三方服务提供商 (CRE Secure,信用卡网关),当我像这样访问请求输入流时:

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

...该流始终具有零字节。

我做错了什么?

I've tried to set up a page in Wicket (extending org.apache.wicket.markup.html.WebPage) to receive an HTTP post request containing an XML document in the body that is coming from a third-party service provider (CRE Secure, a credit card gateway) and when I access the request input stream like this:

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

... the stream always has zero bytes.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-11-04 04:48:54

经过一些调试后,我明白了这一点。 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;

}

After a little debugging, I got to the bottom of this. The InputStream is being consumed by the creation of a PageParameters object. The following method extracts the xml as a string from PageParameters:

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;

}

救星 2024-11-04 04:48:54

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

使用表单元素上的 enctype 属性(例如,text/xml)覆盖默认值会导致请求正文通过 InputStream 可用(尽管它仍然可能是 URLEncoded)。

使用编程源(例如 Apache DefaultHttpClient)并设置 Content-Type 标头还可以让您选择帖子正文是在 PageParameters 还是在 InputStream 中。

Wicket 1.5 更新

在 Wicket 1.5 中,PageParameters 为空。而是调用

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

The result depends on the Content-Type of the incoming stream. Browsers (e.g. Chrome) by default set the content type to application/x-www-form-urlencoded or similar which results in the PageParameters being loaded.

Overriding the default with the enctype attribute on the form element (e.g. to say text/xml) results in the body of the request being available via the InputStream (although it will still probably be URLEncoded).

Using a programmatic source (e.g. Apache DefaultHttpClient) and setting the Content-Type header also lets you choose whether the body of the post is in PageParameters or the InputStream.

Wicket 1.5 Update

In Wicket 1.5 the PageParameters is empty. Instead call

RequestCycle.get().getRequest().getPostParameters().getParameterValue("xml")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文