golang http、https 、通过代理方式 POST

package utils

import (
    "bytes"
    "encoding/json"
    "io/ioutil"
    "net/http"

    "crypto/tls"
    "log"
    "net/http/cookiejar"
    "net/url"
    "time"
)

func Post(url string, arg interface{}, reply interface{}) (err error) {
    var (
        response *http.Response
        body     []byte
        buf      *bytes.Buffer
    )
    log.Println(arg)
    if arg != nil {
        if b, ok := arg.([]byte); !ok {
            if body, err = json.Marshal(arg); err != nil {
                log.Println(err)
                log.Println(arg)
                return
            }
        } else {
            body = b
        }
    }

    buf = bytes.NewBuffer(body)
    //http.DefaultClient.Timeout = 15 * time.Second
    if response, err = http.Post(url, "application/json;charset=utf-8", buf); err != nil {
        return
    }

    if response != nil {
        defer response.Body.Close()
    } else {
        return
    }

    if body, err = ioutil.ReadAll(response.Body); err != nil {
        return
    }
    log.Printf("[Post]%#v", string(body))
    return json.Unmarshal(body, reply)
}

func HttpsPost(url string, arg interface{}, reply interface{}) (err error) {
    var (
        response *http.Response
        body     []byte
        buf      *bytes.Buffer
    )

    if arg != nil {
        if b, ok := arg.([]byte); !ok {
            if body, err = json.Marshal(arg); err != nil {
                return
            }
        } else {
            body = b
        }
    }

    //    log.Println("body", string(body))
    buf = bytes.NewBuffer(body)
    tr := &http.Transport{
        TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
        DisableCompression: true,
    }
    client := &http.Client{Transport: tr}
    //启用cookie
    client.Jar, _ = cookiejar.New(nil)
    ///log.Println(url)
    if response, err = client.Post(url, "application/json;charset=utf-8", buf); err != nil {
        return
    }

    if response != nil {
        defer response.Body.Close()
    } else {
        return
    }

    if body, err = ioutil.ReadAll(response.Body); err != nil {
        return
    }
    log.Printf("[Post]%#v", string(body))
    return json.Unmarshal(body, reply)
}

func HttpsPostForm(host string, arg url.Values, reply interface{}) (err error) {
    var (
        response *http.Response
        body     []byte
    )

    if arg == nil {
        log.Println("arg = nil")
    }

    tr := &http.Transport{
        TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
        DisableCompression: true,
    }
    client := &http.Client{Transport: tr}
    //启用cookie
    client.Jar, _ = cookiejar.New(nil)
    ///log.Println(url)
    if response, err = client.PostForm(host, arg); err != nil {
        return
    }

    if response != nil {
        defer response.Body.Close()
    } else {
        return
    }

    if body, err = ioutil.ReadAll(response.Body); err != nil {
        return
    }
    log.Printf("[Post]%#v", string(body))
    return json.Unmarshal(body, reply)
}

//使用代理服务的HTTP Post方法
//application/json;charset=utf-8
func ProxyPost(proxy func(_ *http.Request) (*url.URL, error), url string, arg interface{}, reply interface{}) (err error) {

    var (
        response *http.Response
        body     []byte
        buf      *bytes.Buffer
    )

    transport := &http.Transport{Proxy: proxy, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
    client := &http.Client{Transport: transport}

    if arg != nil {

        if b, ok := arg.([]byte); !ok {
            if body, err = json.Marshal(arg); err != nil {
                return
            }
        } else {
            body = b
        }
    }

    buf = bytes.NewBuffer(body)
    //http.DefaultClient.Timeout = 15 * time.Second
    if response, err = client.Post(url, "application/json;charset=utf-8", buf); err != nil {
        return
    }

    if response != nil {

        defer response.Body.Close()
    } else {

        return
    }

    if body, err = ioutil.ReadAll(response.Body); err != nil {
        return
    }

    log.Printf("[Post]%#v", string(body))
    return json.Unmarshal(body, reply)

}

//测试是否能ping通外网
func ProxyPing(proxy func(_ *http.Request) (*url.URL, error), url string) (err error) {
    var (
        response *http.Response
    )
    transport := &http.Transport{Proxy: proxy}

    client := &http.Client{Transport: transport, Timeout: 3 * time.Second}

    if response, err = client.Get(url); err != nil {
        return
    }
    log.Println(response, err)

    //if response != nil {
    //}
    return
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值