Java 如果在打开连接之前声明默认cookiehandler,Cookie将丢失
所以我一直在学习http请求和cookie。谁能向我解释一下,为什么会这样Java 如果在打开连接之前声明默认cookiehandler,Cookie将丢失,java,http,cookies,session-cookies,httpcookie,Java,Http,Cookies,Session Cookies,Httpcookie,所以我一直在学习http请求和cookie。谁能向我解释一下,为什么会这样 public void authenticate()引发异常{ //登录信息 最终字符串username=“123”; 最终字符串密码=“123”; 最后一个字符串urlParameters=“login\u username=“+username+”&login\u password=“+password+”&login=%E2%F5%EE%E4”; //初始化 byte[]postData=urlParameters
public void authenticate()引发异常{
//登录信息
最终字符串username=“123”;
最终字符串密码=“123”;
最后一个字符串urlParameters=“login\u username=“+username+”&login\u password=“+password+”&login=%E2%F5%EE%E4”;
//初始化
byte[]postData=urlParameters.getBytes(StandardCharsets.UTF_8);
String loginURL=“无所谓”;
HttpsURLConnection con=(HttpsURLConnection)新URL(loginURL.openConnection();
net.CookieHandler.setDefault(cm);
cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
添加HttpSpoStrequestParameters(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