真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

Go語言Web編程實現(xiàn)Get和Post請求發(fā)送與解析的方法詳解

本文實例講述了Go語言Web編程實現(xiàn)Get和Post請求發(fā)送與解析的方法。分享給大家供大家參考,具體如下:

創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網技術服務公司,擁有項目成都網站建設、網站設計網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元坊子做網站,已為上家服務,為坊子各地企業(yè)和個人服務,聯(lián)系電話:028-86922220

這是一篇入門文章,通過一個簡單的例子介紹Golang的Web編程主要用到的技術。

文章結構包括:

1. Client-Get 請求
2. Client-Post 請求
3. Server 處理 Get 和 Post 數(shù)據(jù)

在數(shù)據(jù)的封裝中,我們部分采用了json,因而本文也涉及到Golang中json的編碼和解碼。

一、Client-Get

復制代碼 代碼如下:
package main
import (
        "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 main
import (
        "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 main
import (
        "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() //解析參數(shù),默認是不會解析的
        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                         fmt.Println(s.Servers[i].ServerName)
                         fmt.Println(s.Servers[i].ServerIP)
                 }
        }
}

希望本文所述對大家Go語言程序設計有所幫助。


標題名稱:Go語言Web編程實現(xiàn)Get和Post請求發(fā)送與解析的方法詳解
URL地址:http://weahome.cn/article/poopip.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部