【发布时间】:2015-12-24 16:54:29
【问题描述】:
我正在尝试使用 cookie 登录 Steam。尝试了两种方法,第一种是:
URL url = new URL("https://steamcommunity.com/");
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
CookieStore cookieStore = cookieManager.getCookieStore();
HttpCookie steamAuthCookie = new HttpCookie("steamMachineAuth*****************", "SteamMachineAuthValue");
steamAuthCookie.setDomain(".steamcommunity.com");
steamAuthCookie.setPath("/");
HttpCookie steamLogin = new HttpCookie("steamLogin", "SteamLoginCookieValue");
steamLogin.setDomain(".steamcommunity.com");
steamLogin.setPath("/");
cookieStore.add(new URI("https://steamcommunity.com/"), steamAuthCookie);
cookieStore.add(new URI("https://steamcommunity.com/"), steamLogin);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
urlConnection.connect();
urlConnection.getContent();
不起作用,Steam 仍然让我登录 我以为 URLConnection 不支持 CookieManager,所以我找到并尝试了第二种方式:
URL url = new URL("https://steamcommunity.com/");
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.8");
String cookie = "steamMachineAuth*****************=****************************************;steamLogin=**************************************************************";
urlConnection.setRequestProperty("Cookie",cookie);
urlConnection.connect();
urlConnection.getContent();
Steam 仍然让我登录。 坚持下去,甚至不知道如何检查 - 是否发送了 cookie。 发送 cookie 的正确方法是什么?
【问题讨论】:
-
您可能需要设置的不仅仅是一个 cookie。我的浏览器显示了 steamcommunity.com 的 12 个 cookie,包括
steamLogin和steamLoginSecure。 -
我知道要正确登录,我必须有 2 个 cookie - steamLogin 和 SteamMachineAuth。在 Chrome 中对其进行了测试-删除了除这两个之外的所有内容-steam 认出了我,删除了 steamLogin 或 SteamMachineAuth-steam 要求我登录。