|
|
@ -15,8 +15,10 @@ import ( |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type HttpHelper struct { |
|
|
type HttpHelper struct { |
|
|
Url string |
|
|
Url string |
|
|
Token string |
|
|
Token string |
|
|
|
|
|
userName string |
|
|
|
|
|
passWord string |
|
|
|
|
|
|
|
|
client http.Client |
|
|
client http.Client |
|
|
} |
|
|
} |
|
|
@ -24,19 +26,25 @@ type HttpHelper struct { |
|
|
func (the *HttpHelper) Initial() { |
|
|
func (the *HttpHelper) Initial() { |
|
|
the.client = http.Client{} |
|
|
the.client = http.Client{} |
|
|
the.client.Timeout = time.Minute |
|
|
the.client.Timeout = time.Minute |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (the *HttpHelper) SetAuth(userName, passWord string) { |
|
|
|
|
|
the.userName = userName |
|
|
|
|
|
the.passWord = passWord |
|
|
} |
|
|
} |
|
|
func (the *HttpHelper) HttpGet(queryBody string) string { |
|
|
func (the *HttpHelper) HttpGet(queryBody string) string { |
|
|
url := the.Url |
|
|
|
|
|
client := the.client |
|
|
client := the.client |
|
|
req, err := http.NewRequest("GET", url, strings.NewReader(queryBody)) |
|
|
req, err := http.NewRequest("GET", the.Url, strings.NewReader(queryBody)) |
|
|
|
|
|
if the.userName != "" && the.passWord != "" { |
|
|
|
|
|
req.SetBasicAuth(the.userName, the.passWord) |
|
|
|
|
|
} |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
resp, err := client.Do(req) |
|
|
resp, err := client.Do(req) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println(err) |
|
|
fmt.Println(err) |
|
|
} |
|
|
} |
|
|
defer resp.Body.Close() |
|
|
defer resp.Body.Close() |
|
|
log.Println("http get 请求,url", url, " <- code=", resp.StatusCode) |
|
|
log.Println("http get 请求,url", the.Url, " <- code=", resp.StatusCode) |
|
|
body, err := io.ReadAll(resp.Body) |
|
|
body, err := io.ReadAll(resp.Body) |
|
|
return string(body) |
|
|
return string(body) |
|
|
} |
|
|
} |
|
|
@ -65,8 +73,8 @@ func (the *HttpHelper) HttpGetWithHeader(queryBody string, headerMap map[string] |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}(resp) |
|
|
}(resp) |
|
|
log.Println("http get 请求,url", url, " <- code=", resp.StatusCode) |
|
|
|
|
|
body, err := io.ReadAll(resp.Body) |
|
|
body, err := io.ReadAll(resp.Body) |
|
|
|
|
|
log.Printf("http get 请求,url=%s, -> code=%d,respBody=%s", url, resp.StatusCode, body) |
|
|
return string(body) |
|
|
return string(body) |
|
|
} |
|
|
} |
|
|
func (the *HttpHelper) Publish(messageBytes []byte) (string, error) { |
|
|
func (the *HttpHelper) Publish(messageBytes []byte) (string, error) { |
|
|
|