-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnet-http-body.go
38 lines (36 loc) · 985 Bytes
/
net-http-body.go
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"
"strconv"
)
func main() {
client := &http.Client{}
request, err := http.NewRequest("GET", "http://www.hao123.com", nil)
if err != nil {
fmt.Println(err)
}
cookie := &http.Cookie{Name: "userId", Value: strconv.Itoa(12345)}
request.AddCookie(cookie)
request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
request.Header.Set("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3")
request.Header.Set("Accept-Encoding", "gzip,deflate,sdch")
request.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
request.Header.Set("Cache-Control", "max-age=0")
request.Header.Set("Connection", "keep-alive")
response, err := client.Do(request)
if err != nil {
fmt.Println(err)
return
}
defer response.Body.Close()
fmt.Println(response.StatusCode)
if response.StatusCode == 200 {
r, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(r))
}
}