java - Java - 302重定向后的基本网址
问题描述
我想出了以下问题。在我的应用程序中,我需要将 XML 文件发布到服务器并等待它的回复。我已经通过使用 Apache HttpClient 实现了上述目标:
- 创建xml
- 使用 HttpClient 和 Post 将其发布到服务器
服务器最初以 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?任何帮助表示赞赏。谢谢
解决方案
推荐阅读
- python - 如何在 Windows 上使用多个进程为 Flask 应用程序提供服务
- file-upload - 将大文件从多个端点上传到云存储的架构
- r - 我的任务说它完成了,但实际上没有
- ruby - 由于使用 ARGV 而不是 .gets,RSpec 测试失败
- visual-studio-debugging - 无法进入 PresentationFramework
- scala - TwitterServer (Finch/Finagle) 似乎阻止了 api 调用,直到它完成计算
- javascript - 如何循环 Angular FormArray - 当前设置为循环中的最后一项
- google-cloud-data-fusion - Google Cloud Data Fusion - Dataproc 配置突然停止,没有任何错误消息
- c++ - 三角形位置的颜色
- javascript - 如何在 Javascript 中获取地图数组以在下拉列表中打印