我有个问题.. 通过该函数,我正在获取当前的URL ....现在,我有两个条件 当我打印它们时,回声正在发生,但是问题是它没有将我重定向到预期的页面……在第一种情况下,我想重定向到具有消息的当前页面,即localhost / test / index.php ?app_not_f ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我有以下代码将通过HttpUrlConnection
调用服务器。
String response = HttpUtil.submitRequest(json.toJSONString(), "http://ipaddr:port/SessionMgr/validateSession?sessionId=_78998348uthjae3a&showLoginPage=true");
以上几行将调用以下代码:
public static String submitRequest(String request, String **requestUrl**) {
try {
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = conn.getOutputStream();
os.write(request.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
StringBuffer sb = new StringBuffer();
while ((output = br.readLine()) != null) {
sb.append(output);
}
conn.disconnect();
return sb.toString();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return "";
}
requestUrl
将转到下面的servlet:
public class ValidateSessionServlet extends HttpServlet {
String session = req.getParameter(sessionId);
if (session == null) {
// redirect to servlet which will display login page.
response.setContentType("text/html");
String actionUrl = getIpPortUrl(request)
+ PropertyConfig.getInstance().getFromIdPConfig(globalStrings.getCheckSSOSession());
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> \n");
out.write("<html><head><body onload=\"document.forms[0].submit()\">\n");
out.write("<form method=\"POST\" action=\"" + actionUrl + "\">\n");
out.write("<input type=\"hidden\" name=\"locale\" value=\"" + locale + "\"/>\n");
out.write("<input type=\"hidden\" name=\"Sessionrequest\" value=\"" + true + "\"/>\n");
out.write("</form>\n</body>\n</html>\n");
}
}
在上面的代码中,表单应该转到actionUrl中提到的servlet,但是它再次转到步骤(1)中的servlet。
1)我可以知道我们可以在步骤(3)中将上面的html表单提交并提交并重定向到actionUrl中的servlet。
根据上面的代码,我总结了要求。 如果会话为空,我必须将用户重定向到登录页面并对数据库进行验证,然后响应应转到步骤(1),是否可能?
如果您希望HttpUrlConnection
支持重定向,则需要像下面这样设置HttpUrlConnection
:
...
conn.setRequestProperty("User-agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1");
conn.setInstanceFollowRedirects(true);
...
然后,如果您的服务器将您的请求重定向到其他位置, conn
将接收重定向的响应。
为了澄清, setInstanceFollowRedirects(true)
仅指示HTTP重定向是否应由HttpURLConnection实例自动跟随。 在您的特定情况下,您似乎希望根据session
是否为null
(或基于您的特定应用程序逻辑的某些其他条件)重定向到servlet。
正确(以及更多防错解决方案)是检查HTTP 3xx
状态代码案例并手动处理重定向。 这是一个代码片段作为示例:
if (responseStatusCode != HttpURLConnection.HTTP_OK) {
switch(responseStatusCode){
case HttpURLConnection.HTTP_MOVED_TEMP:
// handle 302
case HttpURLConnection.HTTP_MOVED_PERM:
// handle 301
case HttpURLConnection.HTTP_SEE_OTHER:
String newUrl = conn.getHeaderField("Location"); // use redirect url from "Location" header field
String cookies = conn.getHeaderField("Set-Cookie"); // if cookies are needed (i.e. for login)
// manually redirect using a new connection
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("User-agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1");
default:
// handle default (other) case
}
}
上面的代码类似于我用于我的应用程序的用户登录重定向,我发现它很容易调试。 (一般情况下,我会手动处理HTTP状态代码案例,以避免出现错误。)
最后,我建议使用一个好的JSON库,例如json.org来解析你的回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.