[TOC] ## http.Redirect 页码跳转 ``` //第四参数必须为 3xx,否则不会跳转 http.Redirect(w,r,"/file/upload/suc",http.StatusFound) ``` ## url.Values ## Get 请求 ``` func HttpGet(url string) ([]byte, error) { resp, e := http.Get(url) defer resp.Body.Close() if e != nil { return nil, e } bytes, e := ioutil.ReadAll(resp.Body) if e != nil { return nil, e } return bytes, nil } func HttpGetString(url string) (string, error) { resp, e := http.Get(url) defer resp.Body.Close() if e != nil { return "", e } s, e := ioutil.ReadAll(resp.Body) if e != nil { return "", e } return string(s), nil } ``` ## post 请求 ``` func HttpPost(url, body string) ([]byte, error) { resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(body)) if err != nil { return nil, err } defer resp.Body.Close() b, e := ioutil.ReadAll(resp.Body) if e != nil { return nil, e } return b, nil } ``` ``` func httpPostForm(url string, value url.Values) ([]byte, error) { resp, err := http.PostForm(url, value) defer resp.Body.Close() if err != nil { return nil, err } body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil } //使用 //values := url.Values{"key": {"Value"}, "id": {"123"}} values := url.Values{} values.Add("key", "Value") bytes, e := httpPostForm("http://www.baidu.com", values) if e != nil { fmt.Println(e) } ```