zoukankan      html  css  js  c++  java
  • 11.1 Go Http

    11.0 Go Http

    http客户端

    package main
    
    import (
        "fmt"
        "net/http"
        "net/http/httputil"
    )
    
    func main() {
        resp, err := http.Get("https://pythonav.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)
    }
    

    客户端添加请求头

    package main
    
    import (
        "fmt"
        "net/http"
        "net/http/httputil"
    )
    
    func main() {
        request, err := http.NewRequest(http.MethodGet, "http://www.pythonav.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)
    }
    

    1.1. go的标准库

    爬虫常用

    bufio
    log
    encoding/json
    time
    regexp
    strings/math/rand
    

    1.2. 本地go标准库

    godoc -http :8000
    

    1.3. go标准库中文网

    https://studygolang.com/pkgdoc
  • 相关阅读:
    input框限制只能输入正整数、字母、小数、
    css水平垂直居中
    Android开发之旅-获取地理位置的经度和纬度
    Android DDMS应用
    Android开发BUG及解决方法2
    Android开发BUG及解决方法1
    Android开发BUG及解决方法
    Android系统架构
    1.sql简介
    C语言笔试常考知识点
  • 原文地址:https://www.cnblogs.com/open-yang/p/11256945.html
Copyright © 2011-2022 走看看