如果我在打开连接之前声明默认的Cookie处理程序,则Cookie会丢失

时间:2019-05-02 14:54:01

标签: java http cookies session-cookies httpcookie

所以我一直在学习http请求和cookie。谁能向我解释为什么这行得通

public void authenticate() throws Exception {        
        // login information
        final String username = "123";
        final String password = "123";
        final String urlParameters = "login_username=" + username + "&login_password=" + password + "&login=%E2%F5%EE%E4";

        // initializing
        byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
        String loginURL = "doesn't matter";
        HttpsURLConnection con = (HttpsURLConnection) new URL(loginURL).openConnection();

        java.net.CookieHandler.setDefault(cm);
        cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

        addHTTPSPOSTRequestParameters(con, postData.length);
        ...

这不是吗?

public void authenticate() throws Exception {        
        java.net.CookieHandler.setDefault(cm);
        cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

        // login information
        final String username = "123";
        final String password = "123";
        final String urlParameters = "login_username=" + username + "&login_password=" + password + "&login=%E2%F5%EE%E4";

        // initializing
        byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
        String loginURL = "doesnt matter";
        HttpsURLConnection con = (HttpsURLConnection) new URL(loginURL).openConnection();

        addHTTPSPOSTRequestParameters(con, postData.length);
        ...

我正在尝试使用post在网页上进行身份验证。 似乎每当我先设置cookiehandler时,第一个返回的cookie都会被吞噬。我获得了认证= HTTP 302-但请求中没有会话cookie。

这是我收到的http回复:

Referrer-Policy: same-origin
Transfer-Encoding: chunked
Server: nginx
Connection: keep-alive
Set-Cookie: bb_session=xxxxxxxxxxxxxxxxxxxxxxxxx; expires=Fri, 27-Apr-2029 15:47:32 GMT; Max-Age=315360000; path=/forum/; domain=.webpage.org; secure; HttpOnly, bb_ssl=1; expires=Fri, 27-Apr-2029 15:47:32 GMT; Max-Age=315360000; path=/forum/; domain=.webpage.org
X-BB-ID: rto
Date: Tue, 30 Apr 2019 15:47:32 GMT
Location: https://webpage.org/123/123.php
Content-Type: text/html; charset=cp1251

这是第一个例子。 如果我使用第二个块中的代码,则会得到以下结果。

Referrer-Policy: same-origin
Transfer-Encoding: chunked
Server: nginx
Connection: keep-alive
Set-Cookie: , bb_ssl=1; expires=Sun, 29-Apr-2029 14:50:23 GMT; Max-Age=315360000; path=/forum/; domain=.webpage.org
X-BB-ID: rto
Date: Thu, 02 May 2019 14:50:23 GMT
Location: https://webpage.org/123/123.php
Content-Type: text/html; charset=cp1251

0 个答案:

没有答案