Go HTTP客户端

Go的http模块可以作为HTTP请求的发起方,来进行HTTP通信。

默认实现

在标准包里,默认提供了GET, POST, PostForm这几种方法的实现。

// 通过GET方式请求
resp, err := http.Get("http://example.com/")

// 通过POST方式请求,上传一张图片
resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)

// 通过POST方式请求,以表单application/x-www-form-urlencoded形式提交
resp, err := http.PostForm("http://example.com/form",
    url.Values{"key": {"Value"}, "id": {"123"}})

注意 客户端在响应完成后必须关闭响应体。如下:

resp, err := http.Get("http://example.com/")
if err != nil {
    // handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

自定义实现

默认的HTTP实现适合于比较简单的场景,如果需要做到像自定义请求头、其它请求方式、重定向等等功能时,就需要进行自定义。

示例:

reqUrl := "http://localhost:8080"
jsonStr := []byte(`{"title":"Buy cheese and bread for breakfast."}`)

// request: POST /url body
req, err := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonStr))
if err != nil {
    return
}
// set content type
req.Header.Set("Content-Type", "application/json")
// add cookie
userCookie := &http.Cookie{
    Name:     "USERID",
    Value:    "ABC",
    Path:     "/",
    HttpOnly: true,
}
req.AddCookie(userCookie)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
    return
}
defer res.Body.Close()

// read response body
body, _ := ioutil.ReadAll(resp.Body)

// do other things ...

results matching ""

    No results matching ""