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

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

问题描述

我想使用 组件向另一台服务器发送 HTTP post 请求.

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

我可以使用 HTML

组件向外部站点发送 POST 请求,但 组件不支持此功能.>

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>

我如何使用 实现这一点?

How can I achieve this with <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.

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

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

更新:根据评论,您实际的问题是您不知道如何处理从您发布的网络服务中获取的 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 提交到网络服务,一旦您获得 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 下载文件?
  • 这篇关于使用 &lt;h:form&gt; 向外部站点发送 HTTP POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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