From 2e9178a53e61094b806c6661b8268924288ca75b Mon Sep 17 00:00:00 2001 From: guzeng Date: Thu, 27 Jan 2022 17:54:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=91=E9=80=81xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http.go | 40 ++++++++++++++++++++++++++++++++++++++++ url.go | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/http.go b/http.go index b1180c3..d448f49 100755 --- a/http.go +++ b/http.go @@ -84,3 +84,43 @@ func SendHttp(method, url string, param map[string]string, header ...map[string] return body, nil } + +/** + * send xml + */ +func SendXml(method, url string, requestxml interface{}, header ...map[string]string) ([]byte, error) { + + bytexml, err := xml.Marshal(&requestxml) + if err != nil { + return []byte(""), err + } + + httpClient := &http.Client{} + + req, err := http.NewRequest(method, url, bytes.NewBuffer(param)) + if err != nil { + return []byte(""), err + } + + req.Header.Add("Content-Type", "application/xml; charset=utf-8") + + if len(header) > 0 { + for _, item := range header { + for k, v := range item { + req.Header.Add(k, v) + } + } + } + resp, err := httpClient.Do(req) + if err != nil { + return []byte(""), err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return []byte(""), err + } + + return body, nil +} diff --git a/url.go b/url.go index 09a43c4..b5dd246 100755 --- a/url.go +++ b/url.go @@ -100,7 +100,7 @@ func PostJsonData(url string, param interface{}, header ...map[string]string) ([ if len(header) > 0 { for _, item := range header { for k, v := range item { - req.Header.Set(k, v) + req.Header[k] = []string{v} } } }