1

我的场景:我正在使用一个对我来说是封闭源代码的企业门户,该应用程序提供了一些报告,我需要在电视中显示报告,所以我需要,电视有一个内置的网络浏览器来显示html。直到这里没有问题,但门户网站有一个登录页面以防止匿名访问,我有凭据,但电视会随机显示报告,所以我需要一种机制来登录应用程序然后加载示例报告并将其传递给电视的浏览器。

我所做的: 我创建了一个网页,它获取报告的 URL,然后登录到门户网站,然后读取 cookie,然后打开与报告的连接(通过它的 URL)以加载其内容并返回内容到浏览器(电视浏览器)。

我的代码

  @WebServlet("/LoadReport")
    public class LoadReport extends HttpServlet {
    private static final long serialVersionUID = 1L;    
    public LoadReport() {
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        HttpSession session = request.getSession(true);     
        session.setMaxInactiveInterval(5);
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();     

        if (request.getQueryString() != null && request.getQueryString() != "") {

            String tmp = request.getQueryString();
            tmp = tmp.substring(tmp.indexOf('=') + 1);
            sb.append(tmp);
            String postParams = "j_username=djboobo&j_password=djboobo&persistent=on";

            URL url = new URL("http://xyz/xyz/login/auth.jsp");
            HttpURLConnection connection = (HttpURLConnection) getConnection(postParams, url);
            // get sessionid from cookies 
            String cookies = getCookies(connection, postParams);            
            cookies += "logindetails=username:djboobo&persistent:yes";
            // url to report
            url = new URL(tmp);
            connection = (HttpURLConnection) getConnection(postParams, url);
            connection.setRequestProperty("Cookie", cookies);
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(postParams);          
            wr.flush();

            try {

                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = br.readLine()) != null) {                    
                    out.write(line);                    
                }               

            } catch (Exception e) {             
            }
            wr.close();
            connection.disconnect();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
    }   

    private HttpURLConnection getConnection(String postParams, URL url) throws IOException, ProtocolException {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Host", "http://xyz/xyz");
        connection.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8,fa;q=0.6");
        connection.setRequestProperty("Cache-Control", "max-age=0");
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Content-Length", String.valueOf(postParams.getBytes().length));
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("charset", "utf-8");
        connection
                .setRequestProperty("User-Agent",
                        "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36");

        return connection;
    }

    private String getCookies(HttpURLConnection conn, String postParams) throws IOException {
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();
        List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
        StringBuilder sb = new StringBuilder();
        for (String cookie : cookies) {
            // System.out.println("Cookies: " + cookie);
            if (cookie.contains("JSESSIONID")) {
                sb.append(cookie.substring(0, cookie.indexOf(";")));
                sb.append("; ");
                break;
            }
        }
        return sb.toString();
    }
    }

主要问题是什么 我尝试使用 chrome 开发人员工具来了解门户的工作方式以及请求和响应 cookie 是什么然后我发现如果我将 SessionId(cookie 之一)传递给门户它将返回报告的内容但是当我使用从 chrome 工具复制的 SessionId(意味着从 chrome 浏览器登录)报告的内容从门户加载,但是当我使用来自以编程方式登录的 SessionId 时,报告的内容没有被加载。

我认为门户登录连接来自程序,因为它返回 SessionId 但登录后它会终止 Session,因此使用 SessionId 报告的连接未通过身份验证。

怎么了 ???

4

0 回答 0