0

我们有一些表格需要我处理,然后发布给第 3 方。我使用 Apache HTTPClient 制作了一个 servlet 来执行此操作。效果很好。我需要它来处理带有文件的多部分编码请求和非多部分编码请求,所以这就是我将其映射到 /ProcessForm 的内容:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String errorPage = "/";
    URLEncoder encoder = new URLEncoder();
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    // See what kinda request we have
    if(ServletFileUpload.isMultipartContent(request)) {
        // Handle the multi-part data
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            List<FileItem> items = upload.parseRequest(request);
            for(FileItem item : items) {
                if(item.isFormField()) {
                    // Parse non-file field
                    String name = item.getFieldName();
                    String val = item.getString();
                    entity.addPart(name, new StringBody(val));
                } else {
                    // Parse file-field
                    String fieldName = item.getFieldName();
                        String fileName = item.getName();
                    byte[] data = item.get();
                    ContentBody fileBody = new ByteArrayBody(data, fileName);
                    entity.addPart(fieldName, fileBody);
                }
            }
        } catch(Exception e) {
            Logger.error(this, "Exception Parsing Form", e);
            response.sendRedirect(errorPage);
            return;
        }
    } else {
        // Handle a non-multipart form
        Enumeration<String> parameterNames = request.getParameterNames();
        for (; parameterNames.hasMoreElements();) {
            String name = parameterNames.nextElement();
            String[] vals = request.getParameterValues(name);
            for(String val : vals){
                entity.addPart(name, new StringBody(val));
            }
        }
    } 

    // DO SOME PROCESSING WITH THE DATA ...

    String actionPath = "https://some-third-party-site.com";

    // Post the data to the action Path
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
            boolean isRedirect=false;
            try {
                isRedirect = super.isRedirected(request, response, context);
            } catch (Exception e) {
                Logger.debug(this,"Some Exception checking for redirect",e);
            }
            if (!isRedirect) {
                int responseCode = response.getStatusLine().getStatusCode();
                if (responseCode == 301 || responseCode == 302) return true;
            }
            return isRedirect;
        }
        });
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    HttpPost httpPost = new HttpPost(actionPath);
    httpPost.getParams().setParameter("http.protocol.handle-redirects",true);
    httpPost.setEntity(entity);
    String resp = "";
    try {
        resp = httpclient.execute(httpPost, responseHandler);
    } catch(Exception e) {
        Logger.error(this, "Exception Posting Form", e);                
        httpPost.releaseConnection();               
        response.sendRedirect(errorPage);
        return;
    }

    PrintWriter out = response.getWriter();
    out.write(resp);
    out.flush();

    out.close();
    httpPost.releaseConnection();
    return;
}

这很好用——问题是对第三方的请求是由我的 servlet “代理”的,而权力不喜欢这样。他们不希望最终的 url 是https://our-site.com/ProcessForm他们希望它是https://the-third-party-site.com/whatever

我一直在试图弄清楚如何做到这一点,到目前为止我还没有想出一种可行的方法。它必须能够处理文件上传,所以我不能只是重定向响应。

我确信我只是没有将这些点与一些明显的东西联系起来,而且我可能过于复杂了。感谢任何帮助。

4

0 回答 0