Go http簡單使用

//包體引入
import "net/http"
  • get
func main() {

    url := "http://www.baidu.com"
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    fmt.Println(resp.Status)
    body , err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(len(string(body)))
}
  • post
func main() {
    body := "{\"title\":\"xxxx\"}"
    response, err := http.Post("xxxx", "application/json",bytes.NewBuffer([]byte(body)))
    if err != nil {
        fmt.Println("net http post method err ,", err)
    }
    
    defer response.Body.Close()
    rlt, _ := ioutil.ReadAll(response.Body)
    fmt.Println(string(rlt))
    
}
  • postform
func main() {
    resp, err := http.PostForm("https://accounts.douban.com/j/mobile/login/basic",url.Values{"name": {"zhangsan@xx.com"}, "password": {"12356"}})
    fmt.Println(resp.Request.URL)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(body))
}
  • Client
//NewRequest 使用給定的Method,URL 和可選的BODY參數(shù),返回一個(gè)新的Request,適用于使用Client.Do或者Transport.RoundTrip。 如果method參數(shù)為空字符串,默認(rèn)使用Get方法。
func main() {
    client := &http.Client{
        Timeout:3 *  time.Second,
    }
    req, err := http.NewRequest("GET","http://www.baidu.com",nil )
    req.Header.Add("X-Requested-With", "XMLHttpRequest")

    if err != nil {
        fmt.Println(err)
    }
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(body))
}
  • Server
//單路由服務(wù)
func main() {
    http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, world...")
    }))
    http.ListenAndServe(":8080", nil)
}
//多路復(fù)用 ServeMux,可以配置多條路由規(guī)則
//不要忘記將mux 注冊(cè)到http里面
type apiHandler struct{}
func (apiHandler) ServeHTTP(w http.ResponseWriter,  r *http.Request){
    w.Write([]byte("hello world"))
}
func main() {
    mux := http.NewServeMux()
    mux.Handle("/",apiHandler{})//只要實(shí)現(xiàn)了Handler接口就可以
    mux.Handle("/hello",http.HandlerFunc(func(w http.ResponseWriter,r *http.Request){
        fmt.Fprint(w,"hello, boy...")
    }))
    http.ListenAndServe(":8080",mux )
}
  • ServerListener
//方式一
func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Welcome...")
    })
    server := http.Server{
        Addr:    ":8080",
        Handler: mux,
    }
    server.ListenAndServe()
}
//方式二
func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Welcome...")
    })
    http.ListenAndServe(":8080",mux)
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容