How to get cookies with Java?

后端 未结 2 648
鱼传尺愫
鱼传尺愫 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: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 cookies = connection.getHeaderFields().get("Set-Cookie");
    // ...
    

提交回复
热议问题