Golang Web编程的Get和Post请求发送与解析
来源:互联网 发布:自动配色软件 编辑:程序博客网 时间:2024/06/05 13:34
本文的是一篇入门文章,通过一个简单的例子介绍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)}
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) } } }
0 0
- Golang Web编程的Get和Post请求发送与解析
- Golang Web编程的Get和Post请求发送与解析
- Golang Web编程的Get和Post请求发送与解析
- HttpClient发送get post请求和数据解析
- HTTP的post和get请求解析
- [Golang]Http get和post请求示例
- golang 中get和post请求详解
- JS发送Get请求和Post请求
- web浏览器发送请求之Get,Post
- C#发送Web请求(GET/POST)
- Web编程----get和post的区别
- android 客户端与服务端的通信 发送get和post请求并获取数据
- Ajax发送GET和POST请求的方法
- Android入门:发送HTTP的GET和POST请求
- Android入门:发送HTTP的GET和POST请求
- httpclient4 发送http请求的get和post用法
- Android入门:发送HTTP的GET和POST请求
- Android入门:发送HTTP的GET和POST请求
- STL::map
- DomHelper
- 面包屑制作
- Move To View、Align With View、Align View to Selected三者的区别
- InitializeCriticalSectionAndSpinCount identifier not found
- Golang Web编程的Get和Post请求发送与解析
- 如何描述产品缺陷
- 【线段树 + 扫描线】poj 1177 pictures 矩形周长并
- 华为面试题:用递归的方法判断整数组a[N]是不是升序排列
- 不靠投资人,也没员工,白手起家到月入7.5万美元——Talkroute的3个成功秘诀
- 对系统和底层结构感兴趣
- 多线程下的神奇的IOCP
- Spark源码阅读笔记:Spark的数据系统之cache篇
- Adapterdemo