golang http
http 挂载方法
-
Head 发送 HEAD 请求
func Head(url string) (resp *Response, err error)
-
Get 发送 GET 请求
func Get(url string) (resp *Response, err error)
res, _ := http.Get("http://www.baidu.com") defer res.Body.Close() if res.StatusCode == 200 { buf, _ := ioutil.ReadAll(res.Body) fmt.Println(string(buf)) }
-
Post 发送 POST 请求
func Post(url string, bodyType string, body io.Reader) (resp *Response, err error)
url := "http://www.baidu.com" res, _ := http.Post(url, "json", nil) buf, _ := ioutil.ReadAll(res.Body) fmt.Println(string(buf))
-
Handle 向 http.DefaultServeMax 注册路由方法
func Handle(pattern string, handler Handler)
type MyHandler string
func (m MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(m))
}
func main() {
var msg MyHandler = "show me"
http.Handle("/", msg)
http.ListenAndServe(":8080", nil)
}
-
HandleFunc 向 http.DefaultServeMax 注册路由方法
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
http.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello Rogan")) }) http.ListenAndServe(":8080", nil)
-
Serve 开启监听服务, 服务底层接口
func Serve(l net.Listener, handler Handler) error
-
ListenAndServe 监听指定地址的 TCP 服务, 既创建 tcp 服务, Serve 的上层接口
func ListenAndServe(addr string, handler Handler) error
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello")) }) http.ListenAndServe(":8080", nil)
-
ListenAndServeTLS 开启 https 服务
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler) error
-
FileServer 开启静态资源服务
func ServeFile(w ResponseWriter, r *Request, name string)
http.Handle("/", http.FileServer(http.Dir("./"))) http.ListenAndServe(":8080", nil)
-
Redirect 重定向
func Redirect(w ResponseWriter, r *Request, urlStr string, code int)
http.HandleFunc("/product", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("to baidu")) http.Redirect(w, r, "http://www.baidu.com", 200) }) http.ListenAndServe(":8080", nil)
-
Error 回复指定错误
func Error(w ResponseWriter, error string, code int)
http.HandleFunc("/product", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "服务器错误", 500) }) http.ListenAndServe(":8080", nil)
-
NotFound 返回404错误
func NotFound(w ResponseWriter, r *Request)
http.HandleFunc("/product", http.NotFound) http.ListenAndServe(":8080", nil)
-
SetCookie 设置Cookie
func SetCookie(w ResponseWriter, cookie *Cookie)
http.HandleFunc("/product", func(w http.ResponseWriter, r *http.Request) { cookie := http.Cookie{ Name: "uId", Value: "111", MaxAge: 100, } // 请求返回前设置 cookie http.SetCookie(w, &cookie) w.Write([]byte("设置 Cookie")) })
Server 服务器挂载方法
- serve 服务监听 与 http.Serve 一致
- ListenAndServe 开启 TCP 服务
- ListenAndServeTLS 开启 HTTPS 服务
Server 服务器属性
- Addr tcp 地址
- Handler ServeMux 处理器
- ReadTImeout 读取超时
- WriteTimeout 写入超时
- MaxHeaderBytes 最大请求头长度
- ConnState 设置日志记录器
- ErrorLog 非导出字段
ServeMux 路由挂载函数
-
NewServeMux 新建路由
func NewServeMux() *ServeMux
-
Handle 挂载处理器
func (mux *ServeMux) Handle(pattern string, handler Handler)
-
HandleFunc 挂载处理器
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request))
-
Handler 返回请求对应的处理器
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string)
-
ServeHTTP 分发请求
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request)
ServeMux and Server 简单例子
router := http.NewServeMux()
server := http.Server{}
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("home page"))
})
server.Addr = ":8080"
server.Handler = router
server.ListenAndServe()
Client 客户端挂载方法
-
Head 发送 Head 请求
func (c *Client) Head(url string) (resp *Response, err error)
-
Get 发送 GET 请求
func (c *Client) Get(url string) (resp *Response, err error)
-
Post 发送 POST 请求
func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *Response, err error)
-
Do 通用请求方法
func (c *Client) Do(req *Request) (resp *Response, err error)
req, _ := http.NewRequest("GET", "http://www.baidu.com", nil) client := &http.Client{} res, _ := client.Do(req) defer res.Body.Close() data, _ := ioutil.ReadAll(res.Body) fmt.Println(string(data))
快捷Handler
-
NotFoundHandler 404
func NotFoundHandler() Handler
-
RedirectHandler 重定向
func RedirectHandler(url string, code int) Handler
TimeoutHandler 超时