Java-302重定向后的基本网址

时间:2020-01-29 08:38:53

标签: java apache http url http-status-code-302

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

  • 创建xml
  • 使用HttpClient和Post将其发布到服务器上

服务器最初响应为302(对象已移动),因此我使用LaxRedirectStrategy来跟踪重定向。我将响应包装在StringBuffer中,然后将其推送到浏览器的新选项卡中。一切正常,该选项卡显示了来自该服务器的完整响应,但是该页面中的每个操作都不与服务器响应的URL关联,而是与我的主应用程序关联。因此,例如,如果我的应用程序在https://myapplication.com上,而服务器在https://theotherserver.com上(对于重定向,则为+ / redirect),则页面上的每个操作(即/ action)都将导致{{3} },而不要https://myapplication.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? 任何帮助表示赞赏。谢谢

0 个答案:

没有答案