Java 使用urlconnection发送cookie

Java 使用urlconnection发送cookie,java,cookies,steam,Java,Cookies,Steam,我正在尝试用cookies登录Steam。尝试了两种方法,第一种是: URL url = new URL("https://steamcommunity.com/"); CookieManager cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); CookieStore cookieStore = cookieManager.getCookieStore(); HttpCookie st

我正在尝试用cookies登录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仍然让我登录。 被它卡住了,甚至不知道如何检查——是否发送了cookies。
发送cookie的正确方法是什么?

安装Wireshark之类的网络嗅探器,手动登录到浏览器中的steam并分析发送的数据。将其与java程序发送的数据逐字节进行比较

安装Wireshark之类的网络嗅探器,手动登录到浏览器中的steam并分析发送的数据。将其与java程序发送的数据逐字节进行比较

谢谢,如果找不到其他解决方案,将执行此操作。谢谢,如果找不到其他解决方案,将执行此操作。您可能需要设置多个cookie。我的浏览器显示了steamcommunity.com的12个cookie,包括
steamLogin
steamLoginSecure
。我知道要正确登录,我必须有两个cookie-steamLogin和SteamMachineAuth。在Chrome中测试-删除了除这两个之外的所有内容-steam认出了我,删除了steamLogin或SteamMachineAuth-steam要求我登录。您可能需要设置的不仅仅是一个cookie。我的浏览器显示了steamcommunity.com的12个cookie,包括
steamLogin
steamLoginSecure
。我知道要正确登录,我必须有两个cookie-steamLogin和SteamMachineAuth。在Chrome中测试它-删除了除这两个之外的所有内容-steam认出了我,删除了steamLogin或SteamMachineAuth-steam要求我登录。