go version go1.15.8 windows/amd64
github.com/gogf/gf v1.15.3
使用BrowserMode
,服务器返回302
的Response
,Client
获取不到Cookies
并保存,redirect
时无法保持登录状态。
实现代码:
c := g.Client().SetBrowserMode(true)
r, err := c.Post(zabbixLoginURL, g.Map{
"name": User,
"password": Password,
"autologin": "0",
"enter": "Sign in",
})
客户端发送POST:
POST /index.php HTTP/1.1
Host: 192.168.126.202
User-Agent: GoFrameHTTPClient v1.15.3
Content-Length: 58
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Connection: close
服务器返回的Response
:
HTTP/1.1 302 Found
Server: nginx
Date: Wed, 03 Mar 2021 02:27:30 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/7.2.11
Set-Cookie: PHPSESSID=8cf0d88a03f1a85ee94edcc63dd5b265; secure; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: zbx_sessionid=36aec56a79ffbcad6d8eb27489f09050; expires=Sat, 03-Apr-2021 02:27:30 GMT; Max-Age=2678400; secure; HttpOnly
Location: zabbix.php?action=dashboard.view
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
Content-Security-Policy-Report-Only: default-src https:; script-src https: 'unsafe-eval' 'unsafe-inline'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /csp-report
Content-Length: 0
客户端重定向发送的GET
:
GET /zabbix.php?action=dashboard.view HTTP/1.1
Host: 192.168.126.202
User-Agent: GoFrameHTTPClient v1.15.3
Content-Type: application/x-www-form-urlencoded
Referer: https://192.168.126.202/index.php
Accept-Encoding: gzip, deflate
Connection: close
查看了文档,BrowserMode
的说明是“Whether auto saving and sending cookie content.”
如果理解无误,在登录正常时,应该能保存服务器返回的Cookies
,并在后续请求中发送已保存的Cookies