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参数,返回一个新的Request,适用于使用Client.Do或者Transport.RoundTrip。 如果method参数为空字符串,默认使用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
//单路由服务
func main() {
    http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, world...")
    }))
    http.ListenAndServe(":8080", nil)
}
//多路复用 ServeMux,可以配置多条路由规则
//不要忘记将mux 注册到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{})//只要实现了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)
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容