55 "encoding/json"
66 "errors"
77 "io"
8+ "io/ioutil"
89 "mime/multipart"
910 "net/http"
1011 "net/url"
@@ -14,6 +15,14 @@ import (
1415 "time"
1516)
1617
18+ const (
19+ ContentTypeAXWFU = "application/x-www-form-urlencoded"
20+ ContentTypeMFD = "multipart/form-data"
21+ ContentTypeTX = "text/xml"
22+ ContentTypeJson = "application/json"
23+ UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
24+ )
25+
1726// 下载文件
1827func DownFile (url , upPreDir , upDir string , proxyURL string ) (string , error ) {
1928 fileType := url [strings .LastIndex (url , "." ):]
@@ -44,8 +53,6 @@ func DownFile(url, upPreDir, upDir string, proxyURL string) (string, error) {
4453 return uploadDir + newName , err
4554}
4655
47- const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
48-
4956// HttpGet获取指定的资源。如果是,则返回ErrNotFound
5057// 服务器以状态404响应。
5158func HttpProxyGet (rawurl string , header http.Header , proxyURL string ) (io.ReadCloser , error ) {
@@ -111,9 +118,9 @@ func HttpRequest(method, urlText, contentType string, params, header map[string]
111118 var err error
112119 var body io.Reader
113120 if params != nil {
114- if method == "POST" {
121+ if method == http . MethodPost {
115122 switch contentType {
116- case "axwfu" : // application/x-www-form-urlencoded;
123+ case ContentTypeAXWFU : // application/x-www-form-urlencoded;
117124 data := make (url.Values )
118125 //data := url.Values{}
119126 for k , v := range params {
@@ -122,7 +129,7 @@ func HttpRequest(method, urlText, contentType string, params, header map[string]
122129 }
123130 body = strings .NewReader (data .Encode ())
124131 contentType = "application/x-www-form-urlencoded; charset=utf-8"
125- case "mfd" : // multipart/form-data
132+ case ContentTypeMFD : // multipart/form-data
126133 bodyBuf := & bytes.Buffer {}
127134 writer := multipart .NewWriter (bodyBuf )
128135 for k , v := range params {
@@ -135,7 +142,7 @@ func HttpRequest(method, urlText, contentType string, params, header map[string]
135142 }
136143 body = bodyBuf
137144 contentType = writer .FormDataContentType ()
138- case "tx" : // text/xml
145+ case ContentTypeTX : // text/xml
139146 data := url.Values {}
140147 for k , v := range params {
141148 data .Set (k , strings .ReplaceAll (v , " " , "+" ))
@@ -169,7 +176,7 @@ func HttpRequest(method, urlText, contentType string, params, header map[string]
169176 req .Header .Add (key , value )
170177 }
171178 }
172- if req .Header .Get ("Content-Type" ) == "" && method == "POST" {
179+ if req .Header .Get ("Content-Type" ) == "" && method == http . MethodPost {
173180 req .Header .Set ("Content-Type" , contentType )
174181 }
175182 if req .Header .Get ("User-Agent" ) == "" {
@@ -182,3 +189,54 @@ func HttpRequest(method, urlText, contentType string, params, header map[string]
182189 // 发起请求
183190 return client .Do (req )
184191}
192+
193+ // 请求并读取返回内容为字符串
194+ func HttpReadBodyString (method , urlText , contentType string , params , header map [string ]string ) (string , error ) {
195+ res , err := HttpRequest (method , urlText , contentType , params , header )
196+ if err != nil {
197+ return "" , err
198+ }
199+ result , err := ioutil .ReadAll (res .Body )
200+ if err != nil {
201+ return "" , err
202+ }
203+ return string (result ), nil
204+ }
205+
206+ // 请求并读取返回内容为json
207+ func HttpReadBodyJson (method , urlText , contentType string , params , header map [string ]string ) (map [string ]interface {}, error ) {
208+ res , err := HttpRequest (method , urlText , contentType , params , header )
209+ if err != nil {
210+ return nil , err
211+ }
212+ result , err := ioutil .ReadAll (res .Body )
213+ if err != nil {
214+ return nil , err
215+ }
216+ var data map [string ]interface {}
217+ err = json .Unmarshal (result , & data )
218+ if err != nil {
219+ return nil , err
220+ }
221+ return data , nil
222+ }
223+
224+ type HttpClient struct {
225+ Method string
226+ UrlText string
227+ ContentType string
228+ Params map [string ]string
229+ Header map [string ]string
230+ }
231+
232+ func (hc * HttpClient ) ReadBody () (* http.Response , error ) {
233+ return HttpRequest (hc .Method , hc .UrlText , hc .ContentType , hc .Params , hc .Header )
234+ }
235+
236+ func (hc * HttpClient ) ReadBodyString () (string , error ) {
237+ return HttpReadBodyString (hc .Method , hc .UrlText , hc .ContentType , hc .Params , hc .Header )
238+ }
239+
240+ func (hc * HttpClient ) ReadBodyJson () (map [string ]interface {}, error ) {
241+ return HttpReadBodyJson (hc .Method , hc .UrlText , hc .ContentType , hc .Params , hc .Header )
242+ }
0 commit comments