zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    bugku 求getshell
    HTTP之content-type
    web之robots.txt
    HTTP之User-Agent大全
    bugku 细心
    PHP输入流
    bugku web8
    PHP中sha1()函数和md5()函数的绕过
    bugku 各种·绕过
    【学术篇】烧水问题 打表找规律做法
  • 原文地址:https://www.cnblogs.com/rxbook/p/7484862.html
Copyright © 2011-2022 走看看