go语言post请求遭遇403反爬解决tls/ja3指纹或Cloudflare防护

一、解决办法

使用tls-client库,请求示例如下:

bash 复制代码
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"time"

	http "github.com/bogdanfinn/fhttp"
	tls_client "github.com/bogdanfinn/tls-client"
	"github.com/bogdanfinn/tls-client/profiles"
)

func main() {
	url := "https://xxxxx.com/abc/ddd"

	// 构造 JSON body
	data := map[string]interface{}{
		"aa":     map[string]interface{}{},
		"bb":       map[string]interface{}{"we": 30, "ty": 0},
		"aaaa": "",
	}
	jsonBytes, _ := json.Marshal(data)

	// 创建 tls-client
	client, err := tls_client.NewHttpClient(
		tls_client.NewNoopLogger(),
		tls_client.WithTimeoutSeconds(25),
		tls_client.WithClientProfile(profiles.Okhttp4Android11),
		tls_client.WithProxyUrl("http://127.0.0.1:7890"), // 代理按需保留
	)
	if err != nil {
		panic(err)
	}

	// 创建请求
	req, err := http.NewRequest("POST", url, bytes.NewReader(jsonBytes))
	if err != nil {
		panic(err)
	}

	// 设置必要头(保持简洁,便于排查)
	req.Header = http.Header{}
	req.Header.Add("Content-Type", "application/json; charset=utf-8")


	start := time.Now()
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	b, _ := io.ReadAll(resp.Body)
	fmt.Println("状态码:", resp.StatusCode)
	fmt.Println("耗时:", time.Since(start))
	fmt.Println("响应头:", resp.Header)
	fmt.Println("响应体:", string(b))
}
相关推荐
码事漫谈2 小时前
当AI开始“思考”:我们是否真的准备好了?
前端·后端
014-code3 小时前
订单超时取消与库存回滚的完整实现(延迟任务 + 状态机)
java·开发语言
lly2024063 小时前
组合模式(Composite Pattern)
开发语言
游乐码4 小时前
c#泛型约束
开发语言·c#
Dontla4 小时前
go语言Windows安装教程(安装go安装Golang安装)(GOPATH、Go Modules)
开发语言·windows·golang
chushiyunen4 小时前
python rest请求、requests
开发语言·python
铁东博客4 小时前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
baidu_huihui4 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip
南 阳4 小时前
Python从入门到精通day63
开发语言·python
lbb 小魔仙4 小时前
Python_RAG知识库问答系统实战指南
开发语言·python