首先使用 http.handlefunc进行相应路由以及路由后的函数处理,即在handlefunc写上路由参数和具体处理方法。然后进行监听。
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type BaseJsonBean struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Message string `json:"message"`
}
func NewBaseJsonBean() *BaseJsonBean {
return &BaseJsonBean{}
}
func main() {
fmt.Println("This is webserver base!")
//第一个参数为客户端发起http请求时的接口名,第二个参数是一个func,负责处理这个请求。
http.HandleFunc("/login", loginTask)
//服务器要监听的主机地址和端口号
err := http.ListenAndServe("localhost:8081", nil)
if err != nil {
fmt.Println("ListenAndServe error: ", err.Error())
}
}
func loginTask(w http.ResponseWriter, req *http.Request) {
fmt.Println("loginTask is running...")
//模拟延时
time.Sleep(time.Second * 2)
//获取客户端通过GET/POST方式传递的参数
req.ParseForm()
param_userName, found1 := req.Form["userName"]
param_password, found2 := req.Form["password"]
if !(found1 && found2) {
fmt.Fprint(w, "请勿非法访问")
return
}
result := NewBaseJsonBean()
userName := param_userName[0]
password := param_password[0]
s := "userName:" + userName + ",password:" + password
fmt.Println(s)
if userName == "zhangsan" && password == "123456" {
result.Code = 100
result.Message = "登录成功"
} else {
result.Code = 101
result.Message = "用户名或密码不正确"
}
//向客户端返回JSON数据
bytes, _ := json.Marshal(result)
fmt.Fprint(w, string(bytes))
}
注解:下面代码可以对json进行优化
song := make(map[string]interface{})
song[“name”] = “李白”
song[“timelength”] = 128
song[“author”] = “wilson”
bytesData, err := json.Marshal(song)
if err != nil {
fmt.Println(err.Error() )
return
}
post提交数据
服务端
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
fmt.Println("This is webserver base!")
http.HandleFunc("/login", loginTask)
//服务器要监听的主机地址和端口号
err := http.ListenAndServe("localhost:8003", nil)
if err != nil {
fmt.Println("ListenAndServe error: ", err.Error())
}
}
func loginTask(writer http.ResponseWriter, request *http.Request) {
fmt.Println("loginTask")
value := request.FormValue("link")
fmt.Println("value:",value)
mainBody, e := ioutil.ReadAll(request.Body)
if e!=nil {
fmt.Println(e)
}
e = ioutil.WriteFile(value+".txt", mainBody, 0666)
if e!=nil {
fmt.Println(e)
}
}
客户端
package main
import (
"fmt"
"net/http"
"strings"
)
func main() {
data :=`{"type":"10","msg":"hello."}`
request, _ := http.NewRequest("POST", "http://localhost:8003/login?link=888", strings.NewReader(data))
resp,err :=http.DefaultClient.Do(request)
if err!=nil {
fmt.Println(err)
}
fmt.Println(resp.Body)
}
可以在服务器端进行数据保存,并以客户端传过来的参数作为文件的名字。