Files
ciimsproxy/internal/http.go
T

50 lines
1.1 KiB
Go
Raw Normal View History

2020-05-11 14:58:22 +08:00
package internal
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"time"
)
const (
defaultContentType string = "text/xml; charset=UTF-8"
defaultAgent string = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; XFire Client +http://xfire.codehaus.org)"
)
//Post xml string to url
func post(url string, contentType string, msg string) (*http.Response, error) {
return http.Post(url, contentType, bytes.NewBuffer([]byte(msg)))
}
2020-09-18 17:21:00 +08:00
func postSim(url string, msg string) (string, error) {
2020-05-11 14:58:22 +08:00
timeout := time.Duration(10 * time.Second)
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(msg))
if err != nil {
2020-09-18 17:21:00 +08:00
return err.Error(), err
2020-05-11 14:58:22 +08:00
}
req.Header.Set("Content-Type", defaultContentType)
req.Header.Set("User-Agent", defaultAgent)
req.Header.Set("SOAPAction", "")
resp, err := client.Do(req)
if err != nil {
2020-09-18 17:21:00 +08:00
return err.Error(), err
2020-05-11 14:58:22 +08:00
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
2020-09-18 17:21:00 +08:00
return err.Error(), err
2020-05-11 14:58:22 +08:00
}
2020-09-18 17:21:00 +08:00
return string(respBytes), nil
2020-05-11 14:58:22 +08:00
}
//Send message
2020-09-18 17:21:00 +08:00
func Send(url string, message string) (string, error) {
2020-05-11 14:58:22 +08:00
return postSim(url, message)
}