golang学习笔记 --- HTTP 客户端

Get 函数
语法:

Get(url string) (resp *Response, err error)

参数:
字符串类型的 url 地址,需要注意的是这里要是完整地址,要加上 http:// 或 https:// 的地址


返回值:

  • *Response 如果获取到了数据,会将数据保存在 Response 中
  • error 如果请求数据的时候出现错误,会返回一个 error ,并将具体的错误记录到 error 中
package main

import (
    "net/http"
    "log"
    "io/ioutil"
    "os"
    "fmt"
)

const url  = "https://broqiang.com"

func main() {
    // 方式一,直接通过 Get 函数
    resp, err := http.Get(url)
    ErrPrint(err)
    defer resp.Body.Close()

    // 拿到数据
    bytes, err := ioutil.ReadAll(resp.Body)
    ErrPrint(err)

    // 这里要格式化再输出,因为 ReadAll 返回的是字节切片
    fmt.Println("------------- 方法一 ---------------")
    fmt.Printf("%s",bytes)

    // 方式二,通过 client 结构体的 Get 方法
    client := new(http.Client)
    //
    client = &http.Client{}

    resp, err = client.Get(url)
    ErrPrint(err)
    defer resp.Body.Close()

    res, err := ioutil.ReadAll(resp.Body)
    ErrPrint(err)
    fmt.Println("


------------- 方法二 ---------------")
    fmt.Printf("%s",res)
}

func ErrPrint(err error)  {
    if err != nil {
        log.Fatalln(err)
        os.Exit(1)
    }
}

Post
这个方法也比较简单,就是通过 Post 方式和服务器端交互。

POST 函数
语法:

(url string, contentType string, body io.Reader) (resp *Response, err error)

参数:

  • 字符串类型的 url 地址,需要注意的是这里要是完整地址,要加上 http:// 或 https:// 的地址
  • 字符串类型的 contentType ,Post 提交数据的类型,常见的有下面 4 种:
    • application/x-www-form-urlencoded 不设置 enctype 属性的原生 form 表单提交方式。
    • multipart/form-data 上传文件时的数据提交方式,相当于 form 表单的 enctype 等于 multipart/form-data 。
    • application/json 用来告诉服务端消息主体是序列化后的 JSON 字符串。
    • text/xml 它是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范,和 json 作用类型。
  • 实现了 io.Reader 接口的数据。 如: 可以通过 strings.NewReader() 方法将普通字符串实现 io.Reader 接口。

返回值:

  • *Response 如果获取到了数据,会将数据保存在 Response 中
  • error 如果请求数据的时候出现错误,会返回一个 error ,并将具体的错误记录到 error 

server.go

package main

import (
    "net/http"
    "fmt"
    "log"
)

func main() {
    http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
        if req.Method == "POST" {
            fmt.Fprintf(w,"Hello %s
",req.FormValue("name"))
            return
        }

        http.NotFound(w,req)
    })

    log.Fatalf("%v",http.ListenAndServe("localhost:8080",nil))
}

client.go

package main

import (
    "log"
    "os"
    "net/http"
    "strings"
    "fmt"
    "io/ioutil"
    "io"
)

const url  = "http://localhost:8080/hello"

func main() {
    // 方式一,直接通过 Post 函数
    fmt.Println("------------- 方法一 ---------------")
    resp, err := http.Post(url,"application/x-www-form-urlencoded",
        strings.NewReader("name=Bro Qiang"))
    ErrPrint(err)
    defer resp.Body.Close()

    DataPrint(resp.Body)

    // 方式二,通过 client 结构体中的 Post 方法
    fmt.Println("------------- 方法二 ---------------")
    client := &http.Client{}
    resp, err = client.Post(url,"application/x-www-form-urlencoded",
        strings.NewReader("name=New Bro Qiang"))
    ErrPrint(err)
    defer resp.Body.Close()

    DataPrint(resp.Body)
}

func DataPrint(body io.ReadCloser) {
    // 拿到数据
    bytes, err := ioutil.ReadAll(body)
    ErrPrint(err)

    // 这里要格式化再输出,因为 ReadAll 返回的是字节切片
    fmt.Printf("%s",bytes)
}

func ErrPrint(err error)  {
    if err != nil {
        log.Fatalln(err)
        os.Exit(1)
    }
}

另外一种方式 PostForm 方法

可以通过 client 结构体的 PostForm () 方法获取数据,其实两种方式是一样的,PostForm () 函数也是调用的结构体中的 PostForm () 方法。详细的使用可以见示例中的用法

package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "net/http"
    "os"
)

const url = "http://localhost:8080/form"

func main() {
    data := map[string][]string{"name": {"Bro Qiang"}, "gender": {"male"}}

    // 方法一:PostForm 函数
    resp, err := http.PostForm(url, data)
    ErrPrint(err)
    defer resp.Body.Close()

    DataPrint(resp.Body)

    // 方法二:client 结构体的 PostForm 方法
    client := &http.Client{}
    resp, err = client.PostForm(url, data)
    ErrPrint(err)
    defer resp.Body.Close()

    DataPrint(resp.Body)
}

func DataPrint(body io.ReadCloser) {
    // 拿到数据
    bytes, err := ioutil.ReadAll(body)
    ErrPrint(err)

    // 这里要格式化再输出,因为 ReadAll 返回的是字节切片
    fmt.Printf("%s
", bytes)
}

func ErrPrint(err error) {
    if err != nil {
        log.Fatalln(err)
        os.Exit(1)
    }
}
原文地址:https://www.cnblogs.com/saryli/p/13360796.html