1
This commit is contained in:
parent
c832144308
commit
03ffea28fe
31
httpcli.go
31
httpcli.go
@ -7,11 +7,40 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTP_OPT_ADD_HEADER = iota
|
||||||
|
HTTP_OPT_SET_PROXY = iota
|
||||||
|
)
|
||||||
|
|
||||||
|
func internalSetOpt(client *http.Client, request *http.Request, opt int32, key *XValue, val *XValue) {
|
||||||
|
switch opt {
|
||||||
|
case HTTP_OPT_ADD_HEADER:
|
||||||
|
request.Header.Add(key.GetString(), val.GetString())
|
||||||
|
default:
|
||||||
|
panic("error http opt")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func HttpGet(url string, params *XObject) (string, error) {
|
func HttpGet(url string, params *XObject) (string, error) {
|
||||||
|
return HttpGetEx(url, params, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpGetEx(url string, params *XObject,
|
||||||
|
initFunc func(setOpt func(int32, *XValue, *XValue))) (string, error) {
|
||||||
if !StrContains(url, "?") {
|
if !StrContains(url, "?") {
|
||||||
url = url + "?"
|
url = url + "?"
|
||||||
}
|
}
|
||||||
if resp, err := http.Get(url); err == nil {
|
client := &http.Client{}
|
||||||
|
request, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
panic("http.NewRequest error")
|
||||||
|
}
|
||||||
|
if initFunc != nil {
|
||||||
|
initFunc(func (opt int32, key *XValue, val *XValue) {
|
||||||
|
internalSetOpt(client, request, opt, key, val)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if resp, err := client.Do(request); err == nil {
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
return "", errors.New("HttpGet error Status:" + resp.Status)
|
return "", errors.New("HttpGet error Status:" + resp.Status)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user