2017-06-21 36 views
0

我需要創建一個將所有傳入數據(XML)發送到另一個服務器的java proxyservlet。 但是,如何將傳入數據發佈到遠程服務器?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   
} 

回答

0

最後我能解決這個問題,用this article幫助:

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(); 
    } 
0

就像使用CloseableHttpClient更新爲Java7或使用HttpClient過舊的Java版本一樣簡單?

然後將您的OutPutStream讀入Byte Array並寫入CloseableHttpClient的InputStream?