2015-12-24 39 views
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的正确方法是什么?

+0

您可能需要设置的不仅仅是那一个cookie。我的浏览器为steamcommunity.com显示12个cookie,包括'steamLogin'和'steamLoginSecure'。 – VGR

+0

我知道,要正确登录,我必须有2个cookie - steamLogin和SteamMachineAuth。在Chrome中进行了测试 - 删除了除这两个以外的所有内容 - 蒸汽识别出我,删除了steamLogin或SteamMachineAuth - 蒸汽要求我登录。 – Azi

回答

0

安装像Wireshark这样的网络嗅探器,手动登录浏览器并分析发送的数据。将它与由java程序发送的数据进行比较,字节在字节之后。

+0

谢谢,如果没有其他解决方案将被发现,将这样做。 – Azi