Golang:如何使用cookie跟随重定向

huangapple go评论80阅读模式
英文:

Golang: how to follow location with cookie

问题

如果HTTP请求的响应是一个带有cookie的重定向(HTTP代码302),你可以如何指示你的Go客户端跟随新的位置并携带接收到的cookie?

在Go中,你可以通过设置以下参数来实现类似的功能:

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/cookiejar"
	"net/url"
)

func main() {
	// 创建一个cookie jar来存储cookie
	cookieJar, _ := cookiejar.New(nil)

	// 创建一个http client,并设置cookie jar
	client := &http.Client{
		Jar: cookieJar,
	}

	// 创建一个GET请求
	req, _ := http.NewRequest("GET", "http://example.com", nil)

	// 发送请求
	resp, _ := client.Do(req)

	// 检查响应的状态码
	if resp.StatusCode == http.StatusFound {
		// 获取重定向的URL
		redirectURL, _ := resp.Location()

		// 创建一个新的GET请求,带上之前接收到的cookie
		req, _ = http.NewRequest("GET", redirectURL.String(), nil)

		// 发送请求
		resp, _ = client.Do(req)
	}

	// 读取响应的内容
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

上述代码中,我们使用了net/http包和net/http/cookiejar包来实现跟随重定向并携带cookie的功能。首先,我们创建了一个cookie jar来存储cookie。然后,我们创建了一个http client,并将cookie jar设置为client的jar属性。接下来,我们发送一个GET请求,并检查响应的状态码。如果状态码是302(重定向),我们获取重定向的URL,并创建一个新的GET请求,带上之前接收到的cookie。最后,我们发送这个新的请求,并读取响应的内容。

请注意,上述代码中的错误处理部分被省略了,你可以根据实际情况进行错误处理。

英文:

In case the response to an http request is a redirection (http code 302) with a 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:<br>

 COOKIEFILE = &quot;&quot;
 AUTOREFERER = 1
 FOLLOWLOCATION = 1

how can you do that in Go?

答案1

得分: 34

使用Go 1.1,你可以使用net/http/cookiejar来实现这个功能。

下面是一个可工作的示例:

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))
}

希望对你有帮助!

英文:

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

Here is a working example:

package main

import (
	&quot;golang.org/x/net/publicsuffix&quot;
	&quot;io/ioutil&quot;
	&quot;log&quot;
	&quot;net/http&quot;
	&quot;net/http/cookiejar&quot;
)

func main() {
	options := cookiejar.Options{
		PublicSuffixList: publicsuffix.List,
	}
	jar, err := cookiejar.New(&amp;options)
	if err != nil {
		log.Fatal(err)
	}
	client := http.Client{Jar: jar}
	resp, err := client.Get(&quot;http://dubbelboer.com/302cookie.php&quot;)
	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))
}

huangapple
  • 本文由 发表于 2013年8月24日 10:06:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/18414212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定