原创文章,转载请注明出处
https://qiedd.com/

示例代码

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import (
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
)
func main() {
u := url.Values{}
// 请求参数
u.Add("message", "text")
// 设置cookies
cookies := []*http.Cookie{
{Name: "AAA", Value: "aaa"},
{Name: "BBB", Value: "bbb"},
}
// 使用cookieJar管理cookies
cookieURL, _ := url.Parse("https://httpbin.org")
jar, err := cookiejar.New(nil)
jar.SetCookies(cookieURL, cookies)
// 创建一个客户端
client := &http.Client{
Timeout: 10 * time.Second,
Jar: jar,
}
// 创建一个请求
req, err := http.NewRequest("POST", "https://httpbin.org/post", strings.NewReader(u.Encode()))
if err != nil {
fmt.Println(err)
}
// 添加 Headers
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36")
req.Header.Add("content-type", "application/x-www-form-urlencoded")
// 执行请求
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
// 延迟关闭
defer resp.Body.Close()
// 输出响应
fmt.Println(string(body))
}
package main import ( "fmt" "io" "net/http" "net/http/cookiejar" "net/url" "strings" "time" ) func main() { u := url.Values{} // 请求参数 u.Add("message", "text") // 设置cookies cookies := []*http.Cookie{ {Name: "AAA", Value: "aaa"}, {Name: "BBB", Value: "bbb"}, } // 使用cookieJar管理cookies cookieURL, _ := url.Parse("https://httpbin.org") jar, err := cookiejar.New(nil) jar.SetCookies(cookieURL, cookies) // 创建一个客户端 client := &http.Client{ Timeout: 10 * time.Second, Jar: jar, } // 创建一个请求 req, err := http.NewRequest("POST", "https://httpbin.org/post", strings.NewReader(u.Encode())) if err != nil { fmt.Println(err) } // 添加 Headers req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36") req.Header.Add("content-type", "application/x-www-form-urlencoded") // 执行请求 resp, err := client.Do(req) if err != nil { fmt.Println(err) } // 读取响应 body, err := io.ReadAll(resp.Body) if err != nil { fmt.Println(err) } // 延迟关闭 defer resp.Body.Close() // 输出响应 fmt.Println(string(body)) }
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"strings"
	"time"
)

func main() {
	u := url.Values{}

	// 请求参数
	u.Add("message", "text")

	// 设置cookies
	cookies := []*http.Cookie{
		{Name: "AAA", Value: "aaa"},
		{Name: "BBB", Value: "bbb"},
	}

	// 使用cookieJar管理cookies
	cookieURL, _ := url.Parse("https://httpbin.org")
	jar, err := cookiejar.New(nil)
	jar.SetCookies(cookieURL, cookies)

	// 创建一个客户端
	client := &http.Client{
		Timeout: 10 * time.Second,
		Jar:     jar,
	}

	// 创建一个请求
	req, err := http.NewRequest("POST", "https://httpbin.org/post", strings.NewReader(u.Encode()))
	if err != nil {
		fmt.Println(err)
	}

	// 添加 Headers
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36")
	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	// 执行请求
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
	}

	// 读取响应
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
	}

	// 延迟关闭
	defer resp.Body.Close()

	// 输出响应
	fmt.Println(string(body))
}

响应示例

可以看到,headers 中带了 cookies

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"args": {},
"data": "",
"files": {},
"form": {
"message": "text"
},
"headers": {
"Accept-Encoding": "gzip",
"Content-Length": "12",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "AAA=aaa; BBB=bbb",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-62f0bb57-70b1532d31ad5d297bfe1f33"
},
"json": null,
"origin": "",
"url": "https://httpbin.org/post"
}
{ "args": {}, "data": "", "files": {}, "form": { "message": "text" }, "headers": { "Accept-Encoding": "gzip", "Content-Length": "12", "Content-Type": "application/x-www-form-urlencoded", "Cookie": "AAA=aaa; BBB=bbb", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36", "X-Amzn-Trace-Id": "Root=1-62f0bb57-70b1532d31ad5d297bfe1f33" }, "json": null, "origin": "", "url": "https://httpbin.org/post" }
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "message": "text"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "12", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Cookie": "AAA=aaa; BBB=bbb", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-62f0bb57-70b1532d31ad5d297bfe1f33"
  }, 
  "json": null, 
  "origin": "", 
  "url": "https://httpbin.org/post"
}
分类: Go

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理