首页 > 解决方案 > Java - 302重定向后的基本网址

问题描述

我想出了以下问题。在我的应用程序中,我需要将 XML 文件发布到服务器并等待它的回复。我已经通过使用 Apache HttpClient 实现了上述目标:

服务器最初以 302(对象已移动)响应,因此我使用 LaxRedirectStrategy 来跟踪重定向。我将响应包装在 StringBuffer 中,然后将其推送到浏览器的新选项卡中。一切正常,选项卡显示来自该服务器的完整响应,但该页面中的每个操作都与服务器响应的 URL 无关,而是与我的主应用程序相关联。因此,例如,如果我的应用程序位于https://myapplication.com并且服务器位于https://theotherserver.com(+ /redirect 用于重定向),则页面上的每个操作(即 /action)都会导致https://myapplication.com/action而不是https://theotherserver.com/action

在我的页面中,我有一个按钮开始此过程。当你点击它时,它会执行:

   jQuery('input#button').on
            (
                'click',
                function ()
                {
                    jQuery.ajax
                    (
                        {
                            url: 'myURL.do',
                            data: {data: mydata},
                            method: 'POST',
                            success: function(data)
                            {
                                if (data.success == 'true')
                                {
                                    var w = window.open('tab', windowname');
                                    w.document.write(data.result);
                                    w.document.close();
                                    w.focus();
                                }
                                else
                                {
                                    alert(data.error);
                                }
                            }
                        });
                    });

.do 执行以下操作:

public ResponseEntity<Map<String, String>> myMethod(HttpServletRequest request, HttpServletResponse response) throws ParserConfigurationException
    {
        Map<String, String> respData = new HashMap<String, String>();
        try
        {
            String myXML = prepareMyXMLFile();   

            String result = sendMyXMLFile(myXML);

            respData.put("success", String.valueOf(true));
            respData.put("result", result);
            return new ResponseEntity<Map<String, String>>(respData, HttpStatus.OK);
        }
        catch (Exception e)
        {
            respData.put("success", String.valueOf(false));
            respData.put("error", String.valueOf(e));
            return new ResponseEntity<Map<String, String>>(respData, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

最后是 sendMyXMLFile():

private String sendMyXML(String myXML)
{
    try
    {
        HttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
        HttpPost post = new HttpPost("myURL");
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("xml", myXML));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);

        BufferedReader rd = new BufferedReader(new 
        InputStreamReader(response.getEntity().getContent()));

        StringBuffer responseResult = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
        {
            responseResult.append(line);
        }
        return responseResult.toString();
     }
     catch (IOException | TransformerException e)
     {
         //log error
     }
     return null;
 }

我需要做什么才能将响应 URL 用作另一个选项卡中的基本 URL?任何帮助表示赞赏。谢谢

标签: javaapachehttpurlhttp-status-code-302

解决方案


推荐阅读