How to get cookies with Java?

后端 未结 2 644
鱼传尺愫
鱼传尺愫 2021-02-05 20:54

How can I get a cookie from a web page using Java? I mean only Java not with Servlets or etc..

相关标签:
2条回答
  • 2021-02-05 21:23

    You can either get the cookies from the header,

    or you can use Apache commons and use their functionality.

    0 讨论(0)
  • 2021-02-05 21:40

    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");
    // ...
    
    0 讨论(0)
提交回复
热议问题