如果对http请求的响应是带有cookie的重定向(http代码302),
如何指示您的Go客户端使用已收到的cookie关注新位置?
在CURL中,可以通过以下方式设置客户端来轻松实现:
COOKIEFILE = ""
AUTOREFERER = 1
FOLLOWLOCATION = 1
Run Code Online (Sandbox Code Playgroud)
你怎么能在Go那样做?
我想自动备份需要登录的网站的网页内容。我尝试通过模拟 POST 请求来登录。但我收到错误:
csrf token: CSRF attack detected
Run Code Online (Sandbox Code Playgroud)
以下是我使用的代码的一些摘录:
func postLoginForm(csrfToken string) {
values := make(url.Values)
values.Set("signin[username]", "myusername")
values.Set("signin[password]", "mypassword")
values.Set("signin[_csrf_token]", csrfToken)
resp, err := http.PostForm("https://spwebservicebm.reaktor.no/admin/nb", values)
dumpHTTPResponse(resp) // show response to STDOUT
}
Run Code Online (Sandbox Code Playgroud)
我通过获取登录页面并扫描名为 的隐藏输入字段来获取 csrf 令牌signin[_csrf_token]。执行此操作的代码的重要部分如下:
// Finds input field named signin[_csrf_token] and returns value as csrfToken
func handleNode(n *html.Node) (csrfToken string, found bool) {
if n.Type == html.ElementNode && n.Data == "input" {
m := make(map[string]string)
for _, attr := range n.Attr {
m[attr.Key] = …Run Code Online (Sandbox Code Playgroud)