Go使用自定义Http Client

https://cloud.tencent.com/developer/article/1900690

默认的client也有连接池

https://www.google.com/search?q=http+golang+%E8%BF%9E%E6%8E%A5%E6%B1%A0&oq=http+golang+%E8%BF%9E%E6%8E%A5%E6%B1%A0&aqs=chrome..69i57j69i65l2.6904j0j1&sourceid=chrome&ie=UTF-8

https://blog.csdn.net/qq_36517296/article/details/120463708

https://tonybai.com/2021/01/08/understand-how-http-package-deal-with-keep-alive-connection/

https://www.google.com/search?q=golang+header+%E9%95%BF%E8%BF%9E%E6%8E%A5+client&newwindow=1&biw=1920&bih=990&sxsrf=AJOqlzWapSmOnbvZJE3X7NUCoytFUP_GzA%3A1675931563274&ei=q6_kY5ipELyhkPIPkKicsAc&ved=0ahUKEwjYqvzLg4j9AhW8EEQIHRAUB3YQ4dUDCA8&uact=5&oq=golang+header+%E9%95%BF%E8%BF%9E%E6%8E%A5+client&gs_lcp=Cgxnd3Mtd2l6LXNlcnAQAzoKCAAQRxDWBBCwA0oECEEYAEoECEYYAFCfA1iSDGCWD2gBcAF4AIAB6QOIAacLkgEHMi0xLjIuMZgBAKABAcgBCsABAQ&sclient=gws-wiz-serp


在Go语言中,可以通过net/http包来创建自定义的HTTP客户端。自定义HTTP客户端允许你配置请求的各种参数,比如超时时间、重定向策略、HTTP代理等。

下面是一个使用自定义HTTP客户端发送HTTP GET请求的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
"fmt"
"io/ioutil"
"net/http"
"time"
)

func main() {
// 创建一个http.Client实例,可以设置客户端的各种属性,比如超时时间
client := &http.Client{
Timeout: time.Second * 10, // 设置10秒的超时时间
}

// 创建一个HTTP请求
// http.NewRequest 方法返回一个*http.Request对象和一个error。在这里我们假设没有错误,所以忽略了error。
req, _ := http.NewRequest("GET", "http://example.com", nil)

// 为请求设置自定义的Header
req.Header.Set("User-Agent", "MyCustomClient/1.0")

// 使用客户端发送请求
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close() // 确保关闭响应体

// 读取响应内容
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}

// 打印响应内容
fmt.Println(string(body))
}

在这个例子中,http.Client结构体被用来创建一个HTTP客户端实例。我们设置了客户端的超时时间为10秒,这意味着如果在10秒内请求没有完成,则会报错。

然后,我们使用http.NewRequest函数创建了一个新的HTTP请求。这个请求是一个GET请求,目标URL是http://example.com。我们还为这个请求设置了一个自定义的User-Agent头。

接下来,我们调用client.Do(req)方法发送请求,并得到一个响应(resp)。最后,我们读取并打印出响应的主体内容。

通过自定义HTTP客户端,你可以控制请求的各种细节,满足特定的需求,比如处理大量的并发请求、配置代理服务器等。



如果我想自定义更复杂的Http Client,比如代理,Authorization,If-Modified-Since,Referer,User-Agent等,要怎么写?

要在Go中创建一个具有代理、AuthorizationIf-Modified-SinceRefererUser-Agent等更复杂功能的自定义HTTP客户端,你可以按以下步骤进行:

1. 导入必要的包

1
2
3
4
5
import (
"net/http"
"net/url"
"time"
)

2. 设置代理

使用http.ProxyURL来设置HTTP客户端的代理:

1
2
3
4
proxyURL, err := url.Parse("http://your-proxy-address:port")
if err != nil {
// 处理错误
}

3. 创建自定义的HTTP客户端

使用http.Transport来配置代理,并使用http.Client来设置自定义的客户端:

1
2
3
4
5
6
7
8
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}

client := &http.Client{
Transport: transport,
Timeout: time.Minute, // 设置请求超时时间
}

4. 创建请求

使用http.NewRequest创建一个新的HTTP请求,并设置请求头:

1
2
3
4
5
6
7
8
9
10
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
// 处理错误
}

// 设置请求头
req.Header.Set("Authorization", "Bearer your-access-token")
req.Header.Set("If-Modified-Since", "Wed, 21 Oct 2015 07:28:00 GMT")
req.Header.Set("Referer", "http://example-referer.com")
req.Header.Set("User-Agent", "CustomUserAgent/1.0")

5. 发送请求

使用自定义的HTTP客户端发送请求:

1
2
3
4
5
6
7
resp, err := client.Do(req)
if err != nil {
// 处理错误
}
defer resp.Body.Close()

// 处理响应...

完整示例

将以上步骤组合起来,得到以下完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main

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

func main() {
// 设置代理
proxyURL, err := url.Parse("http://your-proxy-address:port")
if err != nil {
panic(err)
}

// 创建自定义的HTTP客户端
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}

client := &http.Client{
Transport: transport,
Timeout: time.Minute,
}

// 创建请求
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
panic(err)
}

// 设置请求头
req.Header.Set("Authorization", "Bearer your-access-token")
req.Header.Set("If-Modified-Since", "Wed, 21 Oct 2015 07:28:00 GMT")
req.Header.Set("Referer", "http://example-referer.com")
req.Header.Set("User-Agent", "CustomUserAgent/1.0")

// 发送请求
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

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

fmt.Println(string(body))
}

记得替换http://your-proxy-address:porthttp://example.comBearer your-access-tokenhttp://example-referer.com和其他示例值为你自己的值。这个示例创建了一个自定义的HTTP客户端,设置了代理和各种请求头,然后对指定的URL发起了GET请求。