使用< h:form>将HTTP POST请求发送到外部站点 [英] Send HTTP POST request to external site using <h:form>

查看:168
本文介绍了使用< h:form>将HTTP POST请求发送到外部站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用<h:form>组件将HTTP发布请求发送到另一台服务器.

I want to send a HTTP post request to another server using <h:form> component.

我可以使用HTML <form>组件将POST请求发送到外部站点,但是<h:form>组件不支持此请求.

I can send a POST request to an external site using HTML <form> component, but <h:form> component does not support this.

<form action="http://www.test.ge/get" method="post">
    <input type="text" name="name" value="test"/>
    <input type="submit" value="CALL"/>
</form>

如何使用<h:form>实现这一目标?

How can I achieve this with <h:form>?

推荐答案

无法使用<h:form>提交到另一台服务器. <h:form>默认情况下提交到当前请求URL.同样,它将自动添加额外的隐藏输入字段,例如表单标识符和JSF视图状态.另外,它将更改输入字段名称所代表的请求参数名称.所有这些都使其不适合提交给外部服务器.

It's not possible to use <h:form> to submit to another server. The <h:form> submits by default to the current request URL. Also, it would automatically add extra hidden input fields such as the form identifier and the JSF view state. Also, it would change the request parameter names as represented by input field names. This all would make it insuitable for submitting it to an external server.

只需使用<form>.您可以在JSF页面中完美地使用纯HTML.

Just use <form>. You can perfectly fine use plain HTML in a JSF page.

更新:根据评论,您的实际问题是您不知道如何处理从您发布的Web服务获取的zip文件.您实际上是在错误的方向寻找解决方案.

Update: as per the comments, your actual problem is that you have no idea how to deal with the zip file as obtained from the webservice which you're POSTing to and for which you were actually looking for the solution in the wrong direction.

继续使用JSF <h:form>并使用其通常的客户端API提交到Web服务,一旦获得具有InputStream风格的ZIP文件(请不要按照注释中的说明将其包装为Reader, zip文件是二进制内容,而不是字符内容),只需通过ExternalContext#getResponseOutputStream()将其写入HTTP响应主体,如下所示:

Just keep using JSF <h:form> and submit to the webservice using its usual client API and once you got the ZIP file in flavor of InputStream (please, do not wrap it a Reader as indicated in your comment, a zip file is binary content not character content), just write it to the HTTP response body via ExternalContext#getResponseOutputStream() as follows:

public void submit() throws IOException {
    InputStream zipFile = yourWebServiceClient.submit(someData);
    String fileName = "some.zip";

    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();
    ec.responseReset();
    ec.setResponseContentType("application/zip");
    ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    OutputStream output = ec.getResponseOutputStream();

    try {
        byte[] buffer = new byte[1024];
        for (int length = 0; (length = zipFile.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        try { output.close(); } catch (IOException ignore) {}
        try { zipFile.close(); } catch (IOException ignore) {}
    }

    fc.responseComplete();
}

另请参见:

  • 如何提供从JSF支持bean下载文件?
  • See also:

    • How to provide a file download from a JSF backing bean?
    • 这篇关于使用&lt; h:form&gt;将HTTP POST请求发送到外部站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆