客戶端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地址換成自己的