MENU

Go 学习笔记:net/http库

September 5, 2021 • Read: 241 • Golang

Go语言提供了一个net的库,其中的net/http库提供了比较完整的http协议

GET请求

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-09-05 11:54:49
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

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

func main() {
    httpUrl := "http://httpbin.org/get"
    res, err := http.Get(httpUrl)

    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    defer res.Body.Close() // 延迟关闭

    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    fmt.Println(string(content))
}

GET请求(带param)

get清求可以直接手动拼接字符串,也可以使用net提供的net/url库自动拼接

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-09-05 11:47:21
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func main() {
    httpUrl := "http://httpbin.org/get"
    param := url.Values{}
    param.Set("name", "dio")
    httpUrl2, err := url.ParseRequestURI(httpUrl)
    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    httpUrl2.RawQuery = param.Encode()
    res, err := http.Get(httpUrl2.String())
    
    if err != nil {
        fmt.Println("Failed:", err)
        return
    }
    
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    fmt.Println(string(body))
}

POST请求

当然,你也可以试着POST一下

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-09-05 12:05:09
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strings"
)

func main() {
    httpUrl := "http://httpbin.org/post"
    contentType := "application/x-www-form-urlencoded"
    data := url.Values{}
    data.Set("name", "dio")
    res, err := http.Post(httpUrl, contentType, strings.NewReader(data.Encode()))
    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    defer res.Body.Close()

    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    fmt.Println(string(content))
}

模拟表单FORM

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-09-05 12:30:40
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func main() {
    httpUrl := "http://httpbin.org/post"
    data := url.Values{}
    data.Set("name", "dio")
    res, err := http.PostForm(httpUrl, data)
    if err != nil {
        fmt.Println("Fail", err)
        return
    }
    defer res.Body.Close()
    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    fmt.Println(string(content))
}

自定义 client

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-09-05 12:25:35
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

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

var (
    client = http.Client{
        Transport: &http.Transport{
            DisableKeepAlives: false,
        },
    }
)

func main() {
    httpUrl := "http://httpbin.org/get"
    res, err := client.Get(httpUrl)
    if err != nil {
        fmt.Println("Fail:", err)
        return
    }
    defer res.Body.Close()
    content, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println("Fail:", err)
    }
    fmt.Println(string(content))
}
Archives Tip
QR Code for this page
Tipping QR Code