本文的是一篇入门文章,通过一个简单的例子介绍Golang的Web编程主要用到的技术。
文章结构包括:
ClIEnt-Get 请求ClIEnt-Post请求
Server 处理 Get 和 Post 数据
在数据的封装中,我们部分采用了Json,因而本文也涉及到Golang中Json的编码和解码。
一、ClIEnt-Get
package mainimport ( "fmt" "net/url" "net/http" "io/IoUtil" "log")func main() { u,_ := url.Parse("http://localhost:9001/xiaoyue") q := u.query() q.Set("username","user") q.Set("password","passwd") u.Rawquery = q.Encode() res,err := http.Get(u.String()); if err != nil { log.Fatal(err) return } result,err := IoUtil.ReadAll(res.Body) res.Body.Close() if err != nil { log.Fatal(err) return } fmt.Printf("%s",result)}
二、ClIEnt-Post
package mainimport ( "fmt" "net/url" "net/http" "io/IoUtil" "log" "bytes" "enCoding/Json")type Server struct { Servername string ServerIP string}type Serverslice struct { Servers []Server ServersID string}func main() { var s Serverslice var newServer Server; newServer.Servername = "Guangzhou_VPN"; newServer.ServerIP = "127.0.0.1" s.Servers = append(s.Servers,newServer) s.Servers = append(s.Servers,Server{Servername: "Shanghai_VPN",ServerIP: "127.0.0.2"}) s.Servers = append(s.Servers,Server{Servername: "Beijing_VPN",ServerIP: "127.0.0.3"}) s.ServersID = "team1" b,err := Json.Marshal(s) if err != nil { fmt.Println("Json err:",err) } body := bytes.NewBuffer([]byte(b)) res,err := http.Post("http://localhost:9001/xiaoyue","application/Json;charset=utf-8",body) if err != nil { log.Fatal(err) return } result,err := IoUtil.ReadAll(res.Body) res.Body.Close() if err != nil { log.Fatal(err) return } fmt.Printf("%s",result)}
三、Server
package mainimport ( "fmt" "net/http" "strings" "HTML" "io/IoUtil" "enCoding/Json")type Server struct { Servername string ServerIP string}type Serverslice struct { Servers []Server ServersID string}func main() { http.HandleFunc("/",handler) http.ListenAndServe(":9001",nil)}func handler(w http.ResponseWriter,r *http.Request) { r.ParseForm() //解析参数,默认是不会解析的 fmt.Fprintf(w,"Hi,I love you %s",HTML.Escapestring(r.URL.Path[1:])) if r.Method == "GET" { fmt.Println("method:",r.Method) //获取请求的方法 fmt.Println("username",r.Form["username"]) fmt.Println("password",r.Form["password"]) for k,v := range r.Form { fmt.Print("key:",k,"; ") fmt.Println("val:",strings.Join(v,"")) } } else if r.Method == "POST" { result,_:= IoUtil.ReadAll(r.Body) r.Body.Close() fmt.Printf("%s\n",result) //未知类型的推荐处理方法 var f interface{} Json.Unmarshal(result,&f) m := f.(map[string]interface{}) for k,v := range m { switch vv := v.(type) { case string: fmt.Println(k,"is string",vv) case int: fmt.Println(k,"is int",vv) case float64: fmt.Println(k,"is float64",vv) case []interface{}: fmt.Println(k,"is an array:") for i,u := range vv { fmt.Println(i,u) } default: fmt.Println(k,"is of a type I don't kNow how to handle") } } //结构已知,解析到结构体 var s Serverslice; Json.Unmarshal([]byte(result),&s) fmt.Println(s.ServersID); for i:=0; i<len(s.Servers); i++ { fmt.Println(s.Servers[i].Servername) fmt.Println(s.Servers[i].ServerIP) } } }总结
以上是内存溢出为你收集整理的Golang Web编程的Get和Post请求发送与解析全部内容,希望文章能够帮你解决Golang Web编程的Get和Post请求发送与解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)