Golang:如何跟踪cookie的位置 [英] Golang: how to follow location with cookie

查看:282
本文介绍了Golang:如何跟踪cookie的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果对http请求的响应是使用Cookie的重定向(http代码302),则

In case the response to an http request is a redirection (http code 302) with a cookie,

如何指示您的Go客户端跟踪新位置在CURL中收到了Cookie?

how can you instruct your Go client to follow the new location with the cookie that has been received?

,可以通过以下方式轻松实现:

in CURL, this can be easily achieved by setting the client with:

 COOKIEFILE = ""
 AUTOREFERER = 1
 FOLLOWLOCATION = 1

推荐答案

使用Go 1.1,您可以使用 net / http / cookiejar

With Go 1.1 you can use the net/http/cookiejar for that.

这是一个工作示例:

package main

import (
    "golang.org/x/net/publicsuffix"
    "io/ioutil"
    "log"
    "net/http"
    "net/http/cookiejar"
)

func main() {
    options := cookiejar.Options{
        PublicSuffixList: publicsuffix.List,
    }
    jar, err := cookiejar.New(&options)
    if err != nil {
        log.Fatal(err)
    }
    client := http.Client{Jar: jar}
    resp, err := client.Get("http://dubbelboer.com/302cookie.php")
    if err != nil {
        log.Fatal(err)
    }
    data, err := ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    if err != nil {
        log.Fatal(err)
    }
    log.Println(string(data))
}

这篇关于Golang:如何跟踪cookie的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆