go-doc-http

server.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
package main

import (
"ant/aBase/aTime"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)

type User struct {
Name string `json:"name"`
Age int `json:"age"`
}

func handleForm(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

r.ParseForm()
body, _ := ioutil.ReadAll(r.Body)
reqContent := string(body)
fmt.Println("进入handleForm请求:")
fmt.Println("\tPath: ", r.URL.Path)
fmt.Println("\tPort: ", r.Host)
fmt.Println("\tMethod:", r.Method)
fmt.Println("\tHeader:", r.Header)
fmt.Println("\tBody:", r.Body)
fmt.Println("\t\tcontent:", reqContent)
fmt.Println("\tForm: ", r.Form)
fmt.Println("\t\t:", r.Form["a"])
fmt.Println("\t\t:", r.Form["b"])
fmt.Println("\t\t:", r.Form["c"])
for k, v := range r.Form {
fmt.Println("\t\t", k, "=>", v, strings.Join(v, "-"))
}

fmt.Fprint(w, "Form !")
}

func handleUser(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

body, _ := ioutil.ReadAll(r.Body)
reqContent := string(body)
fmt.Println("进入handleUser请求:")
fmt.Println("\tPath: ", r.URL.Path)
fmt.Println("\tPort: ", r.URL.Port())
fmt.Println("\tMethod:", r.Method)
fmt.Println("\tHeader:", r.Header)
fmt.Println("\tBody:", r.Body)
fmt.Println("\t\tcontent:", reqContent)
fmt.Println("\tForm: ", r.Form)


var user User
if err := json.Unmarshal(body, &user); err == nil {
user.Age ++
ret, _ := json.Marshal(user)
w.Write(ret)
} else {
fmt.Println(err)
}
}

func main() {
http.HandleFunc("/", handleForm)
http.HandleFunc("/form/", handleForm)
http.HandleFunc("/user/", handleUser)

srv1 := &http.Server{Addr: ":8080"}
srv2 := &http.Server{Addr: ":8081"}

go func() {
if err := srv1.ListenAndServe(); err != nil {
// cannot panic, because this probably is an intentional close
log.Printf("Httpserver: ListenAndServe() error: %s", err)
}
}()

go func() {
if err := srv2.ListenAndServe(); err != nil {
// cannot panic, because this probably is an intentional close
log.Printf("Httpserver: ListenAndServe() error: %s", err)
}
}()

aTime.SleepSecond(1000000)

//if err := http.ListenAndServe("127.0.0.1:8080", nil); err != nil {
// log.Fatal("ListenAndServe: ", err)
//}
}
client.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
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
package main

import (
"fmt"
"io/ioutil"
// "log"
"net/http"
// "strings"
"bytes"
"encoding/json"
)

type User struct {
Name string `json:"name"`
Age int `json:"age"`
}

func testUrlGet() {
resp, _ := http.Get("http://127.0.0.1:8080/?a=123456&b=aaa&b=bbb")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("testUrlGet 收到回包:", string(body))
}

func testUrlPost() {
var user User
user.Name = "testUrlPost"
user.Age = 99
if bs, err := json.Marshal(user); err == nil {
req := bytes.NewBuffer([]byte(bs))
tmp := `{"name":"testUrlPost", "age": 10}`
req = bytes.NewBuffer([]byte(tmp))

body_type := "application/json;charset=utf-8"
resp, _ := http.Post("http://127.0.0.1:8080/user/", body_type, req)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("testUrlPost 收到回包:", string(body))
} else {
fmt.Println(err)
}
}

func testClientGet() {
client := &http.Client{}
request, _ := http.NewRequest("GET", "http://127.0.0.1:8080/form/?a=123456&b=aaa&b=bbb", nil)
request.Header.Set("Connection", "keep-alive")
response, _ := client.Do(request)
if response.StatusCode == 200 {
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("testClientGet 收到回包:", string(body))
}
}

func testClientPost() {
client := &http.Client{}
req := `{"name":"testClientPost", "age": 20}`
req_new := bytes.NewBuffer([]byte(req))
request, _ := http.NewRequest("POST", "http://127.0.0.1:8080/user/", req_new)
request.Header.Set("Content-type", "application/json")
response, _ := client.Do(request)
if response.StatusCode == 200 {
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("testClientPost 收到回包:", string(body))
}
}

func main() {
testUrlGet()
testClientGet()
testUrlPost()
testClientPost()
}