且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从swing应用程序到servlet进行http调用,会话未保存

更新时间:2023-12-04 14:06:23

servlet会话由cookie支持。您基本上需要从第一个请求的响应中获取所有 Set-Cookie 标头,然后传递 name = value 成对后续请求的 Cookie 标头。

The servlet session is backed by a cookie. You basically need to grab all Set-Cookie headers from the response of the first request and then pass the name=value pairs back as Cookie header of the subsequent requests.

目前还不清楚您使用的是哪个HTTP客户端,但是如果它的 java.net.URLConnection ,然后你可以使用 java.net.CookieHandler

It's unclear what HTTP client you're using, but if it's java.net.URLConnection, then you could use the java.net.CookieHandler for this.

// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...



参见: