通过Java登录Web门户

时间:2014-11-25 15:52:23

标签: java cookies login httpurlconnection missing-cookies

我的方案: 我正在使用一个闭源的企业门户网站,该应用程序提供了一些报告,我需要在电视中显示报告,所以我需要,电视有一个内置的Web浏览器来显示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浏览器中意味着loged-in)报告的内容从门户网站加载时,但是当我使用SessionId来自编程时登录报告&# 39; s内容未加载。

我认为门户登录连接来自程序,因为它返回SessionId,但是在登录后它会终止会话,因此使用SessionId报告的连接没有得到认证。

什么错了???

0 个答案:

没有答案