客户端quick-cocos程序如下:
local info={hero={
heroname="zhanshen",herolevel=1,herohp=200
},
[1]={
name="fengbao",level=2,att=100
}
}
function onRequestFinished(event)
local ok = (event.name == "completed")
local request = event.request
if not ok then
-- 请求失败,显示错误代码和错误消息
print(request:getErrorCode(), request:getErrorMessage())
return
end
local code = request:getResponseStatusCode()
if code ~= 200 then
-- 请求结束,但没有返回 200 响应代码
print(code)
return
end
-- 请求成功,显示服务端返回的内容
local response = request:getResponseString()
print(response)
end
-- 创建一个请求,并以 POST 方式发送数据到服务端
local url = "http://192.168.1.101:8080/test"
local request = network.createHTTPRequest(onRequestFinished, url, "POST")
for key ,value in pairs(info) do
for key,value in pairs(value)do
print(key,value)
request:addPOSTValue(key, value)
end
end
-- 开始请求。当请求完成时会调用 callback() 函数
request:start()
服务端golang程序:
package main
import (
//"encoding/json"
"fmt"
//"io/ioutil"
"net/http"
//"strings"
)
type person struct {
NAME string
AGE int
}
func main() {
http.HandleFunc("/test", hand)
err := http.ListenAndServe("192.168.1.101:8080", nil)
if err != nil {
fmt.Println("err")
}
}
func hand(w http.ResponseWriter, r *http.Request) {
fmt.Println("ok")
w.Write([]byte("connect ok"))
r.ParseForm() //解析参数, 默认是不会解析的
fmt.Println(r.Form) //这些是服务器端的打印信息
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", v) //v的类型是[]string
}
}
记得把IP地址换成自己的
有疑问加站长微信联系(非本文作者)