1
我在尝试用cookie登录Steam。试了2种方式,第一种是:用urlconnection发送cookie
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();
不工作时,蒸汽仍然提供我登录 我认为的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();
仍然有蒸汽让我登录。 卡住了,甚至不知道如何检查 - 是否发送了cookies。 发送cookies的正确方法是什么?
您可能需要设置的不仅仅是那一个cookie。我的浏览器为steamcommunity.com显示12个cookie,包括'steamLogin'和'steamLoginSecure'。 – VGR
我知道,要正确登录,我必须有2个cookie - steamLogin和SteamMachineAuth。在Chrome中进行了测试 - 删除了除这两个以外的所有内容 - 蒸汽识别出我,删除了steamLogin或SteamMachineAuth - 蒸汽要求我登录。 – Azi