http包提供了HTTP客戶端和服務端的實現
一:http客戶端的幾種方法
1、 func (c *Client) Get(url string) (resp *Response, err error)
說明: 利用get方法請求指定的url,Get請求指定的頁面信息,並返回實體主體
2、func (c *Client) Head(url string) (resp *Response, err error)
說明:利用head方法請求指定的url,Head只返回頁面的首部
3、func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *Response, err error)
說明:利用post方法請求指定的URl,如果body也是一個io.Closer,則在請求之后關閉它
4、func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error)
說明:利用post方法請求指定的url,利用data的key和value作為請求體.
5、func (c *Client) Do(req *Request) (resp *Response, err error)
說明:Do發送http請求並且返回一個http響應,遵守client的策略,如重定向,cookies以及auth等.當調用者讀完resp.body之后應該關閉它,
如果resp.body沒有關閉,則Client底層RoundTripper將無法重用存在的TCP連接去服務接下來的請求,如果resp.body非nil,則必須對其進行關閉.
通常來說,經常使用Get,Post,或者PostForm來替代Do
代碼示例
1、http.Get
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { requestUrl := "http://www.baidu.com" response, err := http.Get(requestUrl) if err != nil { fmt.Println(err) } defer response.Body.Close() body, _ := ioutil.ReadAll(response.Body) fmt.Println(string(body)) }
2、http.Post
package main import ( "bytes" "fmt" "io/ioutil" "net/http" "net/url" ) func main() { requestUrl := "http://www.baidu.com/" // request, err := http.Get(requestUrl) // request, err := http.Head(requestUrl) postValue := url.Values{ "username": {"hangmeimei"}, "address": {"anhui"}, "subject": {"world"}, "form": {"beij"}, } //request, err := http.PostForm(requestUrl, postValue) body := bytes.NewBufferString(postValue.Encode()) request, err := http.Post(requestUrl, "text/html", body) if err != nil { fmt.Println(err) } defer request.Body.Close() fmt.Println(request.StatusCode) if request.StatusCode == 200 { rb, err := ioutil.ReadAll(request.Body) if err != nil { fmt.Println(rb) } fmt.Println(string(rb)) } }
3、 http.Do
package main import ( "fmt" "io/ioutil" "net/http" "strconv" ) func main() { client := &http.Client{} request, err := http.NewRequest("GET", "http://www.baidu.com", nil) if err != nil { fmt.Println(err) } cookie := &http.Cookie{Name: "Tom", Value: strconv.Itoa(123)} request.AddCookie(cookie) //向request中添加cookie //設置request的header request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") request.Header.Set("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3") request.Header.Set("Accept-Encoding", "gzip,deflate,sdch") request.Header.Set("Accept-Language", "zh-CN,zh;q=0.8") request.Header.Set("Cache-Control", "max-age=0") request.Header.Set("Connection", "keep-alive") response, err := client.Do(request) if err != nil { fmt.Println(err) return } defer response.Body.Close() fmt.Println(response.StatusCode) if response.StatusCode == 200 { r, err := ioutil.ReadAll(response.Body) if err != nil { fmt.Println(err) } fmt.Println(string(r)) } }
二:建立web服務器
package main import ( "net/http" ) func SayHello(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Hello")) } func main() { http.HandleFunc("/hello", SayHello) http.ListenAndServe(":8080", nil) }
說明:
首先調用Http.HandleFunc
往DefaultServeMux的map[string]muxEntry中增加對應的handler和路由規則
http.ListenAndServe
啟動一個http服務器,監聽8080端口
上面的代碼中蘊含着http服務器處理http的流程,有時間可以看源碼分析分析
參考:
https://www.cnblogs.com/msnsj/p/4365186.html
http://www.infoq.com/cn/articles/golang-standard-library-part02