這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
Get, Head, Post, and PostForm 可以構成 HTTP (or HTTPS) requests:
resp, err := http.Get("http://example.com/")...resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)...resp, err := http.PostForm("http://example.com/form", url.Values{"key": {"Value"}, "id": {"123"}})
用戶端必須關閉response的body:
resp, err := http.Get("http://example.com/")if err != nil { // handle error}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)// ...
TCP用戶端設定逾時的方法:
c := http.Client{ Transport: &http.Transport{ Dial: func(netw, addr string) (net.Conn, error) { deadline := time.Now().Add(25 * time.Second) c, err := net.DialTimeout(netw, addr, time.Second*20) if err != nil { return nil, err } c.SetDeadline(deadline) return c, nil }, }, }
一些用戶端請求的其他設定:
client := &http.Client{ CheckRedirect: redirectPolicyFunc,}resp, err := client.Get("http://example.com")// ...req, err := http.NewRequest("GET", "http://example.com", nil)// ...req.Header.Add("If-None-Match", `W/"wyzzy"`)resp, err := client.Do(req)// ...
要是想在NewRequest裡面帶上post資料,就必須傳入一個io.Reader的body,這個body如果實現了io.Closer,那麼返回的Request.body賦給這個body,然後通過Do或者Post什麼的關閉。這個實現了io.Reader的body,基本上是通過構成?a=1&b=2這樣的query的url.Values轉化的,可以通過url.Values.add和set構建,前者可疊加,後者會覆蓋之前的值,url.Values有一個Encode可以轉化出相應的query字串,然後再通過strings.NewReader()轉換成io.Reader介面,就行了。還可以通過ioutil.NopCloser返回一個帶Closer的io.reader
另外post資料的時候記得帶上頭:
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
下面這個還不知道什麼意思
For control over proxies, TLS configuration, keep-alives, compression, and other settings, create a Transport:
tr := &http.Transport{ TLSClientConfig: &tls.Config{RootCAs: pool}, DisableCompression: true,}client := &http.Client{Transport: tr}resp, err := client.Get("https://example.com")
具體的一個例子:
package mainimport ( "fmt" "io/ioutil" "net/http" "net/url" "net" "time" "os" "strings")// func nothing() {// fmt.Println("nothing")// client := &http.Client{}// a := strings.Contains("a", "b")// u:=&url.URL{}// r, err := u.Parse("baidu.com")// os.Exit(0)// }const ( LHST = "http://127.0.0.1:3000/")func main() { client := &http.Client{ Transport: &http.Transport{ Dial: func(netw, addr string) (net.Conn, error) { deadline := time.Now().Add(25 * time.Second) c, err := net.DialTimeout(netw, addr, time.Second*20) if err != nil { return nil, err } c.SetDeadline(deadline) return c, nil }, }, } form := url.Values{} form.Set("username", "gao") b := strings.NewReader(form.Encode()) req, err := http.NewRequest("POST", LHST+"answer", b) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") res, err := client.Do(req) if err != nil { fmt.Println("Fatal error ", err.Error()) os.Exit(0) } defer res.Body.Close() body,err:=ioutil.ReadAll(res.Body) fmt.Println(string(body))}
再舉個例子
func main() { host := "http://www.minigps.net/l.do?" client := http.Client{} q := url.Values{} q.Set("c", "454") q.Set("n", "6") q.Set("a", "8010") q.Set("e", "6652663") req, _ := http.NewRequest("GET", host+q.Encode(), nil) req.Header.Add("Content-Type","application/json;charset=UTF-8") res, err := client.Do(req) if err != nil { fmt.Println(err) os.Exit(-1) } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body))}