zoukankan      html  css  js  c++  java
  • golang学习笔记 ---http标准库

     1.使用http客户端发送请求

    2.使用http.Client控制请求头部等

     3.使用httpputil简化工作

    package main
     
    import (
        "fmt"
        "net/http"
        "net/http/httputil"
    )
     
    func main() {
        resp, err := http.Get("https://www.imooc.com")
        if err != nil {
            panic(err)
        }
        defer resp.Body.Close()
     
        s, err := httputil.DumpResponse(resp, true)
        if err != nil {
            panic(err)
        }
     
        fmt.Printf("%s
    ",s)
    }

    示例2:

    package main
     
    import (
        "fmt"
        "net/http"
        "net/http/httputil"
    )
     
    func main() {
        request, err := http.NewRequest(
            http.MethodGet,
            "http://www.imooc.com", nil)
        request.Header.Add("User-Agent",
            "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1")
     
        client := http.Client{
            CheckRedirect: func(
                req *http.Request,
                via []*http.Request) error {
                fmt.Println("Redirect:", req)
                return nil
            },
        }
        resp, err := client.Do(request)
        if err != nil {
            panic(err)
        }
        defer resp.Body.Close()
     
        s, err := httputil.DumpResponse(resp, true)
        if err != nil {
            panic(err)
        }
     
        fmt.Printf("%s
    ", s)
    }
  • 相关阅读:
    「CF1039D」You Are Given a Tree
    「NOIP2016」换教室
    「NOIP2014」飞扬的小鸟
    「AMPPZ2014」The Prices
    POj-3104 Drying 二分+贪心
    HDOJ1312<DFS>
    STL入门2
    HDU1425 <sort 快排>
    2304: Lights Out(枚举)
    1018:放苹果(递归)
  • 原文地址:https://www.cnblogs.com/saryli/p/13364475.html
Copyright © 2011-2022 走看看