123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- package httpClient
- import (
- "bytes"
- "crypto/tls"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "strings"
- "time"
- )
- const (
- timeout = 5 * time.Second
- )
- type HttpRequest struct {
- Url string
- Method string
- TimeOut time.Duration
- Values map[string]string
- Bytes []byte
- }
- type HttpRequestWithHead struct {
- Url string
- Method string
- Head map[string]string
- TimeOut time.Duration
- Query map[string]string
- Body []byte
- }
- func HttpPostWithHead(path string, head map[string]string,
- query map[string]string, body []byte) ([]byte, error) {
- if query != nil {
- path += joinGetRequestArgs(query)
- }
- h := HttpRequestWithHead{
- Url: path,
- Method: http.MethodPost,
- Head: head,
- Body: body,
- TimeOut: timeout,
- }
- return h.request()
- }
- func HttpPutWithHead(path string, head map[string]string,
- query map[string]string, body []byte) ([]byte, error) {
- if query != nil {
- path += joinGetRequestArgs(query)
- }
- path += joinGetRequestArgs(query)
- h := HttpRequestWithHead{
- Url: path,
- Method: http.MethodPut,
- Head: head,
- Body: body,
- TimeOut: timeout,
- }
- return h.request()
- }
- func HttpPost(path string, data map[string]string) ([]byte, error) {
- h := HttpRequest{
- Url: path,
- Method: http.MethodPost,
- Values: data,
- TimeOut: timeout,
- }
- return h.request()
- }
- func HttpPostBytes(path string, data []byte) ([]byte, error) {
- h := HttpRequest{
- Url: path,
- Method: http.MethodPost,
- TimeOut: timeout,
- Bytes: data,
- }
- return h.requestBytes()
- }
- func HttpPut(path string, data map[string]string) ([]byte, error) {
- h := HttpRequest{
- Url: path,
- Method: http.MethodPut,
- Values: data,
- TimeOut: timeout,
- }
- return h.request()
- }
- func HttpDelete(path string, data map[string]string) ([]byte, error) {
- h := HttpRequest{
- Url: path,
- Method: http.MethodDelete,
- Values: data,
- TimeOut: timeout,
- }
- return h.request()
- }
- func HttpGet(path string, data map[string]string) ([]byte, error) {
- path += joinGetRequestArgs(data)
- h := HttpRequest{
- Url: path,
- Method: http.MethodGet,
- TimeOut: timeout,
- }
- return h.request()
- }
- func (h HttpRequest) request() ([]byte, error) {
- values := url.Values{}
- if h.Values != nil {
- for k, v := range h.Values {
- values.Add(k, v)
- }
- }
- request, err := http.NewRequest(h.Method, h.Url, strings.NewReader(values.Encode()))
- if h.Method == http.MethodPost ||
- h.Method == http.MethodPut ||
- h.Method == http.MethodDelete {
- request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- }
- //跳过证书验证
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- }
- client := http.Client{
- Transport: tr,
- Timeout: timeout,
- }
- resp, err := client.Do(request)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- contents, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- return contents, nil
- }
- func (h HttpRequestWithHead) request() ([]byte, error) {
- request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Body))
- if h.Method == http.MethodPost ||
- h.Method == http.MethodPut ||
- h.Method == http.MethodDelete {
- request.Header.Add("Content-Type", "application/json")
- }
- for k, v := range h.Head {
- request.Header.Add(k, v)
- }
- //跳过证书验证
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- }
- client := http.Client{
- Transport: tr,
- Timeout: timeout,
- }
- resp, err := client.Do(request)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- contents, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- return contents, nil
- }
- func (h HttpRequest) requestBytes() ([]byte, error) {
- request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Bytes))
- if h.Method == http.MethodPost ||
- h.Method == http.MethodPut ||
- h.Method == http.MethodDelete {
- request.Header.Add("Content-Type", "application/json")
- }
- //跳过证书验证
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- }
- client := http.Client{
- Transport: tr,
- Timeout: timeout,
- }
- resp, err := client.Do(request)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- contents, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- return contents, nil
- }
- func joinGetRequestArgs(data map[string]string) string {
- var path string
- if data != nil {
- path += "?"
- for k, v := range data {
- path += k + "=" + v + "&"
- }
- path = strings.TrimRight(path, "&")
- return path
- }
- return path
- }
|