听起来您可能需要处理cookie标头才能保留会话。如果是这种情况,则这并非特定于HTTPS。
Set-cookie发出第一个请求时,您需要找到响应头。然后,每个请求都将通过
cookie请求标头传递。这是一个可以适应您的情况的基本示例:
// your first request that does the authenticationURL authUrl = new URL("https://example.com/authentication");HttpsURLConnection authCon = (HttpsURLConnection) authUrl.openConnection();authCon.connect();// temporary to build request cookie headerStringBuilder sb = new StringBuilder();// find the cookies in the response header from the first requestList<String> cookies = authCon.getHeaderFields().get("Set-cookie");if (cookies != null) { for (String cookie : cookies) { if (sb.length() > 0) { sb.append("; "); } // only want the first part of the cookie header that has the value String value = cookie.split(";")[0]; sb.append(value); }}// build request cookie header to send on all subsequent requestsString cookieHeader = sb.toString();// with the cookie header your session should be preservedURL regUrl = new URL("https://example.com/register");HttpsURLConnection regCon = (HttpsURLConnection) regUrl.openConnection();regCon.setRequestProperty("cookie", cookieHeader);regCon.connect();


