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