| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package utils
- import (
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- )
- func HttpGet(url string, params map[string]string) (result []byte, err error) {
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- fmt.Println(err.Error())
- return
- }
- q := req.URL.Query()
- for k, v := range params {
- q.Add(k, v)
- }
- req.URL.RawQuery = q.Encode()
- fmt.Println(req.URL.String())
- var resp *http.Response
- resp, err = http.DefaultClient.Do(req)
- if err != nil || resp == nil {
- return
- }
- output, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- result = output
- defer func() {
- if resp != nil {
- resp.Body.Close()
- }
- }()
- return
- }
- func HttpPost(url string, params interface{}) (result []byte, err error) {
- data, _ := json.Marshal(params)
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
- if err != nil {
- return
- }
- fmt.Println(url)
- req.Header.Set("Content-Type", "application/json;charset=UTF-8")
- resp, err := client.Do(req)
- if err != nil {
- return
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- result = body
- return
- }
- func HttpPostReplyCode(url string, params interface{}) (result []byte, code int, err error) {
- data, _ := json.Marshal(params)
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
- if err != nil {
- return
- }
- fmt.Println(url)
- req.Header.Set("Content-Type", "application/json;charset=UTF-8")
- resp, err := client.Do(req)
- if err != nil {
- return
- }
- code = resp.StatusCode
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- result = body
- return
- }
- func HttpGetReplyCode(url string, params map[string]string) (result []byte, code int, err error) {
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- fmt.Println(err.Error())
- return
- }
- q := req.URL.Query()
- for k, v := range params {
- q.Add(k, v)
- }
- req.URL.RawQuery = q.Encode()
- fmt.Println(req.URL.String())
- var resp *http.Response
- resp, err = http.DefaultClient.Do(req)
- if err != nil || resp == nil {
- return
- }
- code = resp.StatusCode
- output, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- result = output
- defer func() {
- if resp != nil {
- resp.Body.Close()
- }
- }()
- return
- }
- func HttpPutReplyCode(url string, params interface{}) (result []byte, code int, err error) {
- data, _ := json.Marshal(params)
- client := &http.Client{}
- req, err := http.NewRequest("PUT", url, strings.NewReader(string(data)))
- if err != nil {
- return
- }
- fmt.Println(url)
- req.Header.Set("Content-Type", "application/json;charset=UTF-8")
- resp, err := client.Do(req)
- if err != nil {
- return
- }
- code = resp.StatusCode
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- result = body
- return
- }
- func HttpDeleteReplyCode(url string, params interface{}) (result []byte, code int, err error) {
- data, _ := json.Marshal(params)
- client := &http.Client{}
- req, err := http.NewRequest("DELETE", url, strings.NewReader(string(data)))
- if err != nil {
- return
- }
- fmt.Println(url)
- req.Header.Set("Content-Type", "application/json;charset=UTF-8")
- resp, err := client.Do(req)
- if err != nil {
- return
- }
- code = resp.StatusCode
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- result = body
- return
- }
- func Request(method, urlPath string, reqBody io.Reader, header map[string]string, cookies []*http.Cookie) (resp *http.Response, err error) {
- req, err := http.NewRequest(method, urlPath, reqBody)
- if err != nil {
- return
- }
- for k, v := range header {
- req.Header.Set(k, v)
- }
- for _, v := range cookies {
- req.AddCookie(v)
- }
- var (
- client = http.Client{
- Timeout: 15 * time.Second,
- //Transport: &http.Transport{Proxy: http.ProxyURL(&url.URL{Host: "localhost:8888"})},
- }
- )
- if resp, err = client.Do(req); err != nil {
- return
- }
- defer resp.Body.Close()
- return
- }
|