http 使用小結
來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。做一個簡單的通過http協議 server 和 client 互動範例。吧內文章很多,但是沒有一個完整的範例,在這裡做一個摸索總結,希望對後來的學習go語言的小夥伴有協助。server端代碼:```gopackage mainimport ("fmt""net/http")func main() {fmt.Println("server listening:")http.HandleFunc("/con",conncet)http.ListenAndServe(":8080",nil)} func conncet(w http.ResponseWriter, r *http.Request) { r.ParseForm() data := r.URL.Query() fmt.Println("all data", data) var NameExist, IdExist bool var Id,Name []string if Id, NameExist = r.Form["Id"]; NameExist{ fmt.Println("Id is ",Id[0]) } if Name, IdExist = r.Form["Name"]; IdExist{ fmt.Println("Name is ",Name[0]) } if NameExist && IdExist{ w.Write([]byte(fmt.Sprintf("welcome %s to come in",Name[0]))) }}```說明: server通過監聽本地的8080連接埠,等待用戶端的串連。先啟動server,列印如下:```C:/Go\bin\go.exe run E:/go_code/gopath/src/example/http/httpserver.goserver listening:all data map[Id:[321] Name:[agent]]Id is 321Name is agentall data map[Name:[agent] Id:[321]]Id is 321Name is agent```client 代碼,client 通過讀取設定檔,擷取服務的ip地址以及訪問的目錄位址。```gopackage mainimport ("fmt""net/http""io/ioutil""encoding/json"//"net/url")type ServerInfo struct {Ip stringPort stringDir string}type ClientInfo struct {Name stringID string}type ConfigData struct { ServerData ServerInfo ClientData ClientInfo}func main() {fmt.Println("client con:")client := &http.Client{}serveInst, clientInst, err := conInst.ServerData.GetServerInfoFromConf()if err != nil{fmt.Println(err)return}url := serveInst.constructUrl()urlpara := clientInst.constructUrl(&url)req, e := http.NewRequest("Get",urlpara, nil)if e !=nil{fmt.Println(e)}//v := req.Form//v.Set("Name",clientInst.Name)//v.Set("Id",clientInst.ID)rsp ,err1 := client.Do(req)if err1 != nil{fmt.Println("client Get err.",err1)return}defer rsp.Body.Close()data, err2 := ioutil.ReadAll(rsp.Body)if err2 == nil {fmt.Println(string(data))}fmt.Println("client over", err2)}var conInst = NewConfInfo()func NewConfInfo() *ConfigData{return &ConfigData{}}func (s *ServerInfo)GetServerInfoFromConf() (*ServerInfo, *ClientInfo,error){conf := &ConfigData{}data, err := ioutil.ReadFile("conf/info.json")if err != nil{fmt.Println("readdir failed, err is ",err)return nil,nil,err}err1 := json.Unmarshal(data,conf)if err1 != nil {fmt.Println("unmarshal failed, err is ",err1)return nil, nil,err1}fmt.Println("Get info is ",conf)return &conf.ServerData, &conf.ClientData,nil}func (s *ServerInfo)constructUrl() string {url := "http://" + s.Ip + ":" + s.Port + "/" + s.Dirfmt.Println("url is",url)return url}func (s *ClientInfo)constructUrl(url *string) string {//*url =*url + "?" + "Name" + "=" + s.Name*url =*url + "?" + "Name" + "=" + s.Name + "&"+ "Id" + "=" + s.IDfmt.Println("url is", *url)return *url}```clinet 列印如下:```C:/Go\bin\go.exe run E:/go_code/gopath/src/example/http/httpclient.goclient con:Get info is &{{127.0.0.1 8080 con} {agent 321}}url is http://127.0.0.1:8080/conurl is http://127.0.0.1:8080/con?Name=agent&Id=321welcome agent to come inclient over <nil>Process finished with exit code 0```json檔案如下:```json{ "ServerData" :{ "Ip" : "127.0.0.1","Port": "8080","Dir" : "con" }, "ClientData" :{ "Name": "agent", "ID": "321" }}```186 次點擊