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
}