如何将 POST 请求从 servlet 重定向到另一个应用程序

发布于 2024-10-15 13:21:21 字数 120 浏览 4 评论 0原文

我们可以将 POST 请求从部署在 server1 中的一个应用程序中的 servlet 重定向到部署在 server2 中的另一个应用程序中的端点吗?是否可以使用 POST 请求而不丢失数据,还是始终需要使用 GET 请求?

Can we redirect a POST request from a servlet in one application deployed in server1 to an endpoint in another application deployed in server2? Is this possible with POST request without loss of data or does it always need to be a GET request?

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

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

发布评论

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

评论(2

野味少女 2024-10-22 13:21:21

几种方法:

  1. 如果您当前的请求已经是 POST 请求,只需显式发送 307 重定向而不是 302 重定向(response.sendRedirect() 默认设置状态代码 302) .

     response.setStatus(307);
     response.setHeader("位置", "https://other.com");
    

    但是,这会在某些网络浏览器的客户端发出安全确认警告。

  2. 自己玩代理游戏。

     URLConnection 连接 = new URL("https://other.com").openConnection();
     连接.setDoOutput(true); // 邮政
     // 如有必要,复制标头。
    
     输入流 input1 = request.getInputStream();
     OutputStream 输出1 = 连接.getOutputStream();
     // 将请求正文从 input1 复制到 output1。
    
     输入流 input2 = 连接.getInputStream();
     OutputStream 输出2 = response.getOutputStream();
     // 将响应正文从 input2 复制到 output2。
    

    但这不会更改 URL,并且客户会认为他仍在您的网站上。另请参阅如何使用 java.net .URLConnection 用于触发和处理 HTTP 请求,了解有关如何使用 URLConnection 的更多详细信息。

  3. 将所有 POST 数据转换为适合 GET 请求的查询字符串,以便仍然可以使用 302 重定向。

     字符串编码 = request.getCharacterEncoding();
    
     如果(编码==空){
         编码= StandardCharsets.UTF_8.name();
         request.setCharacterEncoding(编码);
     }
    
     StringBuilder 查询 = new StringBuilder();
    
     for (Entry 条目 : request.getParameterMap().entrySet()) {
         for (字符串值:entry.getValue()) {
             if (query.length() > 0) {
                 查询.append("&");
             }
    
             query.append(URLEncoder.encode(entry.getKey(), 编码));
             查询.append("=");
             query.append(URLEncoder.encode(值, 编码));
         }
     }
    
     response.sendRedirect("https://other.com?" + 查询);
    

Several ways:

  1. If your current request is already a POST request, just explicitly send a 307 redirect instead of a 302 redirect (the response.sendRedirect() sets by default a status code of 302).

     response.setStatus(307);
     response.setHeader("Location", "https://other.com");
    

    This will however issue a security confirmation warning at the client side in some web browsers.

  2. Play for proxy yourself.

     URLConnection connection = new URL("https://other.com").openConnection();
     connection.setDoOutput(true); // POST
     // Copy headers if necessary.
    
     InputStream input1 = request.getInputStream();
     OutputStream output1 = connection.getOutputStream();
     // Copy request body from input1 to output1.
    
     InputStream input2 = connection.getInputStream();
     OutputStream output2 = response.getOutputStream();
     // Copy response body from input2 to output2.
    

    This will however not change the URL and the client thinks he's still on your site. See also How to use java.net.URLConnection to fire and handle HTTP requests for more detail on how to use URLConnection.

  3. Convert all POST data to a query string suitable for a GET request so that a 302 redirect can be used nonetheless.

     String encoding = request.getCharacterEncoding();
    
     if (encoding == null) {
         encoding = StandardCharsets.UTF_8.name();
         request.setCharacterEncoding(encoding);
     }
    
     StringBuilder query = new StringBuilder();
    
     for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
         for (String value : entry.getValue()) {
             if (query.length() > 0) {
                 query.append("&");
             }
    
             query.append(URLEncoder.encode(entry.getKey(), encoding));
             query.append("=");
             query.append(URLEncoder.encode(value, encoding));
         }
     }
    
     response.sendRedirect("https://other.com?" + query);
    
萌梦深 2024-10-22 13:21:21

是的,您可以使用 HTTPURLConnection< /a>

这里 是示例

try {
    // Construct data
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

    // Send data
    URL url = new URL("http://hostname:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        // Process line...
    }
    wr.close();
    rd.close();
} catch (Exception e) {
//log it ,sms it, mail it
}

Yes you can using HTTPURLConnection

here is example

try {
    // Construct data
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

    // Send data
    URL url = new URL("http://hostname:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        // Process line...
    }
    wr.close();
    rd.close();
} catch (Exception e) {
//log it ,sms it, mail it
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文