0%

golang发送常见请求案例

golang发送常见请求案例

QQ群:397745473

使用golang 发送get post 使用代理的代码片段。

golang 发送GET请求案例 .

参考:https://studygolang.com/pkgdoc

参考: https://www.liwenzhou.com/posts/Go/go_http/

GET请求(方法1)

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
package main

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

const (
targetURL = "http://www.baidu.com/index.php?page=100&c=222"
)

func main() {
// 发送GET请求
resp, err := http.Get(targetURL)
if err != nil {
fmt.Printf("访问出错!%v\n", err)
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("读取数据出错!%v\n", err)
return
}
fmt.Println(string(b))
}

GET请求(方法2)

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
package main

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

const (
targetURL = "https://ipinfo.io"
)

func main() {
// 发送GET请求
data := url.Values{}
urlObj, _ := url.Parse(targetURL)
data.Set("page", "100")
data.Set("c", "222")
querStr := data.Encode() // url encode之后的内容
fmt.Println(querStr)
urlObj.RawQuery = querStr

req, err := http.NewRequest("GET", urlObj.String(), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("获取数据失败: %v", err)
return
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("读数数出错: %v", err)
}
fmt.Println(string(b))

}

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
"compress/gzip"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)

const (
targetURL = "https://ipinfo.io"
)

func main() {
// 发送GET请求
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse("socks5://127.0.0.1:7890")
}

httpTransport := &http.Transport{
//解决x509: certificate signed by unknown authority
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}

httpClient := &http.Client{
Transport: httpTransport, //解决x509: certificate signed by unknown authority
Timeout: time.Second * 10, //超时时间
}

req, err := http.NewRequest("GET", targetURL, nil)
if err != nil {
// handle error
return
}

req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0")
req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("X-Requested-With", "XMLHttpRequest")
req.Header.Set("Connection", "keep-alive")

resp, err := httpClient.Do(req)
if err != nil || resp.StatusCode == 403 || resp.StatusCode == 404 || resp.StatusCode == 503 {
// handle error
// 这两行打日志的生成时取消,不然会打很多日志
//logger.Println(err)
//logger.Println(requestURL)
return
}
defer resp.Body.Close()
body, err := gzip.NewReader(resp.Body) // 解压
result, _ := ioutil.ReadAll(body)
//result, err := ioutil.ReadAll(resp.Body) // 如果服务器开启了gzip,就需要先解压再读取
if err != nil {
// handle error
//logger.Println(err)
return
}
fmt.Println(string(result))

}

POST请求

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
package main

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

const (
targetURL = "https://ipinfo.io"
)

func main() {
// 发送POST请求
contentType := "application/json"
data := `{"name":"小王子","age":18}`
resp, err := http.Post(targetURL, contentType, strings.NewReader(data))
if err != nil {
fmt.Printf("post failed, err:%v\n", err)
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("get resp failed, err:%v\n", err)
return
}
fmt.Println(string(b))

}

简单自定义POST请求

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
package main

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

const (
targetURL = "https://ipinfo.io"
)

func main() {
// 发送POST请求
req, _ := http.NewRequest("POST", targetURL, strings.NewReader("formhash=测试1&signsubmit=测试2"))
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0")
req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
// req.Header.Set("Accept-Encoding", "gzip, deflate") // 本行导致将 response 结果从 byte转 string 错误,故注释本行,在发包时取消发送 Accept-Encoding 这个http头

client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
readbody, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(readbody))

}

完整的自定义POST方法

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 随机生成IP地址方法
func genIpaddr() string {
rand.Seed(time.Now().Unix())
ip := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
return ip
}


func postUrlResponse(Referer, requestURL, userkey, session string, wg *sync.WaitGroup) (string, string) {
defer wg.Done()
//logger.Println("[GET]=>" + requestURL)
randIp := genIpaddr()
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(PROXY_ADDR)
}

httpTransport := &http.Transport{
//解决x509: certificate signed by unknown authority
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: proxy,
}

//req, err := http.NewRequest("POST", requestURL, nil)

data := url.Values{}
data.Set("data", datas)
data.Set("__k", userkey)
u, _ := url.ParseRequestURI(requestURL)
u.RawQuery = data.Encode()
urlStr := fmt.Sprintf("%v", u) // "https://api.com/user/?name=foo&surname=bar"

httpClient := &http.Client{
Transport: httpTransport, //解决x509: certificate signed by unknown authority
Timeout: time.Second * 10, //超时时间
}

//req, err := http.NewRequest("POST", urlStr, nil)
req, err := http.NewRequest(http.MethodPost, urlStr, strings.NewReader(data.Encode())) // URL-encoded payload
//req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(requestBody))
if err != nil {
// handle error
return "error", "error"
}

req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0")
req.Header.Set("Accept", "application/json, text/javascript")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
// req.Header.Set("Accept-Encoding", "gzip, deflate")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("X-Requested-With", "XMLHttpRequest")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Cookie", "session="+session)
req.Header.Set("Referer", Referer)
req.Header.Set("Pragma", "no-cache")
req.Header.Set("Cache-Control", "no-cache")

req.Header.Set("X-Forward-For", randIp)
req.Header.Set("HTTP_X_FORWARD_FOR", randIp)
req.Header.Set("HTTP_CLIENT_IP", randIp)
req.Header.Set("X-Real-IP", randIp)
req.Header.Set("REMOTE_ADDR", randIp)

resp, err := httpClient.Do(req)
if err != nil || resp.StatusCode == 403 || resp.StatusCode == 404 || resp.StatusCode == 503 {
// handle error
// 这两行打日志的生成时取消,不然会打很多日志
//logger.Println(err)
//logger.Println(requestURL)
return "error", "error"
}
defer resp.Body.Close()
//body, err := gzip.NewReader(resp.Body)
//result, _ := ioutil.ReadAll(body)
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
//logger.Println(err)
return "error", "error"
}
return string(result), datas
}

QQ群:397745473

欢迎关注我的其它发布渠道