簡體   English   中英

使用urlconnection發送cookie

[英]send cookies with urlconnection

我正在嘗試使用Cookie登錄到Steam。 嘗試了2種方法,第一種是:

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的正確方法是什么?

安裝諸如Wireshark之​​類的網絡嗅探器,手動登錄以在瀏覽器中運行並分析發送的數據。 逐字節比較它與Java程序發送的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM