Java proxyservlet 将数据发布到另一台服务器

J000S

我需要创建一个将所有传入数据 (XML) 发送到另一台服务器的 java proxyservlet。但是如何将传入的数据发布到远程服务器?

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String server = "http://server.tld";
    String subURI = request.getRequestURI().split("/ProxyServlet")[1];
    System.out.println("ProxyServlet: " + server + subURI);
    URL remoteServer = new URL(server + subURI);
    HttpURLConnection connection = (HttpURLConnection) remoteServer.openConnection();

    //somehow apply request to remoteServer and receive response        
}
J000S

最后我可以在这篇文章的帮助下解决它

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String server = "http://server.tld";
    String subURI = request.getRequestURI().split("/ProxyServlet")[1];      
    System.out.println("ProxyServlet: " + server + subURI);     
    URL remoteServerURL = new URL(server+subURI);
    HttpURLConnection remoteServer = (HttpURLConnection) remoteServerURL.openConnection();
    remoteServer.setRequestMethod("POST");
    remoteServer.setDoOutput(true);
    remoteServer.getOutputStream().write(readBytes(request.getInputStream()));
    response.getOutputStream().write(readBytes(remoteServer.getInputStream()));
}

 /**
   * Read and return the entire contents of the supplied {@link InputStream stream}. This method always closes the stream when
   * finished reading.
   * 
   * @param stream the stream to the contents; may be null
   * @return the contents, or an empty byte array if the supplied reader is null
   * @throws IOException if there is an error reading the content
   */
  private byte[] readBytes( InputStream stream ) throws IOException {
      if (stream == null) return new byte[] {};
      byte[] buffer = new byte[1024];
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      boolean error = false;
      try {
          int numRead = 0;
          while ((numRead = stream.read(buffer)) > -1) {
              output.write(buffer, 0, numRead);
          }
      } catch (IOException e) {
          error = true; // this error should be thrown, even if there is an error closing stream
          throw e;
      } catch (RuntimeException e) {
          error = true; // this error should be thrown, even if there is an error closing stream
          throw e;
      } finally {
          try {
              stream.close();
          } catch (IOException e) {
              if (!error) throw e;
          }
      }
      output.flush();
      return output.toByteArray();
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过另一台服务器转发 Java HTTP 请求

一台服务器上是否存在Java SSLHandshakeException,而另一台服务器上没有?

java中调用在另一台服务器上运行的C函数的最佳方法是什么?

NodeJS,Axios-将文件从本地服务器发布到另一台服务器

使用node.js,needle,busboy / multer将文件从一台服务器发布到另一台服务器

将一台服务器复制到另一台服务器

什么是使用Java从另一台postgres服务器中插入一百万行到postgresql服务器中的有效方法?

将Postgres数据库从一台服务器复制到另一台服务器

发布到另一台服务器时,AJAX 重新加载网页

bash将数组传递给ssh到另一台服务器

MySQL将流量重定向到另一台服务器

什么时候将网站删除到另一台服务器?

将PostgreSQL数据库复制到另一台服务器

将数据从Django发送到另一台服务器

使用ansible同步将目录从一台服务器复制到另一台服务器到不同的路径

从 JSON API(从一台服务器)获取数据并将其显示在 HTML 上(到另一台服务器)

如何使用PHP从一台服务器接收数据到另一台服务器

Elasticsearch将索引从一台服务器复制到另一台服务器?

如何将一台服务器的ram磁盘同步到另一台服务器

如何将Grafana仪表板完全从一台服务器复制到另一台服务器

将ssh密钥从一台服务器复制到另一台服务器

将 BLOB 从一台服务器复制到另一台服务器

如何使服务器将客户端重定向到另一台服务器?

Apache Ignite-将数据从一台服务器移动到另一台服务器

如何将Redis数据库从一台服务器移动到另一台服务器?

错误1045:将SQL数据库从一台服务器传输到另一台服务器

从另一台服务器上的PHP脚本发布到应用程序

将一台服务器上的PostgreSQL数据库复制到另一台服务器上的MySQL数据库

如何将一台 HornnetQ 服务器的消息备份复制到另一台服务器?