java - HttpUrlConnection 重定向到另一个 servlet 没有发生

标签 java servlets httpurlconnection

我有以下代码将通过 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 吗?

根据上面的代码,我总结了要求。如果 session 为空,我必须将用户重定向到登录页面并根据数据库进行验证,然后响应应该转到步骤 (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 将接收重定向的响应。

关于java - HttpUrlConnection 重定向到另一个 servlet 没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29361316/

相关文章:

java - J2ME Java - 如何获取图像的一部分并将其放大

java - 在java中生成随机数的问题

java - Servlet 路径错误

tomcat - Servlet Security transport-guarantee "CONFIDENTIAL"重定向后不显示请求的页面

java - Facebook Messenger 不发送消息

java - Log4java zip 压缩问题 : All file appenders are the same but compression only works for some of them

java - GWT 刷新编译周期

Java URL/HttpURLConnection 如何在发布时避免 InputStream?

java - httpurl连接线程安全

java - 不知道发生异常时如何转发404页面