• golang的GET请求(类似于PHP的CURL)


    check_url := "https://www.baidu.com"
    
        header := make(map[string]string)
    
        res, err := util.Hpool.Request(check_url, http.MethodGet, "", header)
        if err != nil {
            return nil, err
        }
    
        
        spew.Dump(res)

    【http.go】

    package util
    
    import (
        "bytes"
        "io/ioutil"
        "net/http"
        "time"
    )
    
    type HttpConPool struct {
        Conn *http.Client
    }
    
    var Hpool *HttpConPool
    
    func loadHttpPool() {
        Hpool = new(HttpConPool)
        Hpool.Conn = &http.Client{
            Transport: &http.Transport{
                MaxIdleConnsPerHost: Str2Int(GetConfigValue("http", "max_conn")),
            },
            Timeout: time.Duration(Str2Int(GetConfigValue("http", "timeout"))) * time.Millisecond,
        }
    }
    
    func (h *HttpConPool) Request(url string, method string, data string, header map[string]string) (interface{}, error) {
        req, err := http.NewRequest(method, url, bytes.NewBuffer([]byte(data)))
        if err != nil {
            return nil, err
        }
    
        for h, v := range header {
            req.Header.Set(h, v)
        }
    
        response, err := h.Conn.Do(req)
    
        if err != nil {
            return nil, err
        } else if response != nil {
            defer response.Body.Close()
    
            r_body, err := ioutil.ReadAll(response.Body)
            if err != nil {
                return nil, err
            } else {
                return string(r_body), nil
            }
        } else {
            return nil, nil
        }
    }
  • 相关阅读:
    关于++i和i++的左值、右值问题
    运算符优先级
    计算机中的数及其编码
    递归函数
    PHP读取excel(4)
    替换元素节点replaceChild()
    子结点childNodes
    插入节点insertBefore()
    创建节点createElement
    插入节点appendChild()
  • 原文地址:https://www.cnblogs.com/rxbook/p/7484862.html
Copyright © 2020-2023  润新知