开发者

How to get cookies with Java?

开发者 https://www.devze.com 2023-03-04 13:30 出处:网络
How can I get a cookie from a web page using Java? I mean only Java not with Servlets or开发者_如何学Python etc..You can use java.net.URLConnection for this. It offers a getHeaderFields() method to ge

How can I get a cookie from a web page using Java? I mean only Java not with Servlets or开发者_如何学Python etc..


You can use java.net.URLConnection for this. It offers a getHeaderFields() method to get the response headers. The cookies are set by Set-Cookie header.

URLConnection connection = new URL("http://google.com").openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...


You can either get the cookies from the header,

or you can use Apache commons and use their functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消