302重定向后的Java基url
我提出了以下问题。在我的应用程序中,我需要将一个XML文件发布到服务器并等待它的回复。我通过使用Apache HttpClient实现了上述目标:302重定向后的Java基url,java,apache,http,url,http-status-code-302,Java,Apache,Http,Url,Http Status Code 302,我提出了以下问题。在我的应用程序中,我需要将一个XML文件发布到服务器并等待它的回复。我通过使用Apache HttpClient实现了上述目标: 创建xml 使用HttpClient和Post将其发布到服务器 服务器最初的响应是302(对象移动),所以我使用了laxrirectstrategy来跟踪重定向。我将响应包装在StringBuffer中,然后将其推入浏览器的新选项卡中。一切正常,选项卡显示来自该服务器的完整响应,但该页面中的每个操作都与服务器响应的URL无关,而是与我的主应用程序
- 创建xml
- 使用HttpClient和Post将其发布到服务器
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);
}
}
public ResponseEntity myMethod(HttpServletRequest请求,HttpServletResponse响应)引发ParserConfiguration异常
{
Map respData=newhashmap();
尝试
{
字符串myXML=prepareMyXMLFile();
字符串结果=sendMyXMLFile(myXML);
respData.put(“成功”,String.valueOf(true));
respData.put(“结果”,result);
返回新的ResponseEntity(respData,HttpStatus.OK);
}
捕获(例外e)
{
respData.put(“成功”,String.valueOf(false));
respData.put(“错误”,String.valueOf(e));
返回新的ResponseEntity(respData,HttpStatus.INTERNAL_SERVER_ERROR);
}
}
最后发送myxmlfile():
私有字符串sendMyXML(字符串myXML)
{
尝试
{
HttpClient client=HttpClientBuilder.create().setRedirectStrategy(新的laxrirectStrategy()).build();
HttpPost=新的HttpPost(“myURL”);
List urlParameters=new ArrayList();
添加(新的BasicNameValuePair(“xml”,myXML));
setEntity(新的UrlEncodedFormEntity(urlParameters));
HttpResponse response=client.execute(post);
BufferedReader rd=新的BufferedReader(新的
InputStreamReader(response.getEntity().getContent());
StringBuffer responseResult=新的StringBuffer();
字符串行=”;
而((line=rd.readLine())!=null)
{
应答结果追加(行);
}
返回responseResult.toString();
}
捕获(IOException | TransformerException e)
{
//日志错误
}
返回null;
}
我需要做什么才能将响应URL用作其他选项卡中的基本URL?
感谢您的帮助。谢谢您好,发布问题时最好的做法是添加所有有助于了解当前情况的代码。添加到问题中的代码
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;
}