|
@@ -213,9 +213,15 @@ func (v *RequestPromise) SetClient(client *http.Client) *RequestPromise {
|
|
|
// 发起请求并返回响应内容
|
|
|
// FORM方式提交数据请设置Content-Type=application/x-www-form-urlencoded请求头,且io.Reader传url.Values.Encode得到的字符串的reader
|
|
|
func (v *RequestPromise) Call(httpMethod string, targetUri string, data io.Reader) ([]byte, error) {
|
|
|
+ body, _, err := v.CallResponse(httpMethod, targetUri, data)
|
|
|
+ return body, err
|
|
|
+}
|
|
|
+
|
|
|
+// 发起请求并返回响应内容和响应对象引用
|
|
|
+func (v *RequestPromise) CallResponse(httpMethod string, targetUri string, data io.Reader) ([]byte, *http.Response, error) {
|
|
|
targetUri = strings.TrimSpace(targetUri)
|
|
|
if len(targetUri) == 0 {
|
|
|
- return nil, nil
|
|
|
+ return nil, nil, nil
|
|
|
}
|
|
|
|
|
|
// http request handle
|
|
@@ -226,7 +232,7 @@ func (v *RequestPromise) Call(httpMethod string, targetUri string, data io.Reade
|
|
|
}
|
|
|
req, err := http.NewRequest(httpMethod, targetUri, data)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
if nil != v.headers {
|
|
|
req.Header = v.headers
|
|
@@ -237,10 +243,12 @@ func (v *RequestPromise) Call(httpMethod string, targetUri string, data io.Reade
|
|
|
// send http request & get http response
|
|
|
resp, err := v.client.Do(req)
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
|
|
|
- return v.readResponseBody(resp)
|
|
|
+ var body []byte
|
|
|
+ body, err = v.readResponseBody(resp)
|
|
|
+ return body, resp, err
|
|
|
}
|
|
|
|
|
|
func (v *RequestPromise) initClient() {
|
|
@@ -312,6 +320,16 @@ func (v *RequestPromise) readResponseBody(resp *http.Response) ([]byte, error) {
|
|
|
}
|
|
|
if v.encoding != "" {
|
|
|
body = ConvertToEncodingBytes(body, v.encoding)
|
|
|
+ } else {
|
|
|
+ cType := resp.Header.Get("Content-Type")
|
|
|
+ if cType != "" {
|
|
|
+ cType = strings.ReplaceAll(strings.ToLower(cType), " ", "")
|
|
|
+ if strings.Contains(cType, "charset=gbk") {
|
|
|
+ body = ConvertToEncodingBytes(body, GBK)
|
|
|
+ } else if strings.Contains(cType, "charset=gb18030") {
|
|
|
+ body = ConvertToEncodingBytes(body, GB18030)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return body, nil
|
|
|
}
|