Jerry 6 лет назад
Родитель
Сommit
daf18e9bab
2 измененных файлов с 5 добавлено и 3 удалено
  1. 2 1
      http_client.go
  2. 3 2
      wechat_service_api.go

+ 2 - 1
http_client.go

@@ -6,6 +6,7 @@ import (
 	"encoding/xml"
 	"errors"
 	"fmt"
+	"io"
 	"io/ioutil"
 	"net/http"
 	"strings"
@@ -192,7 +193,7 @@ func (c *Client) EndBytes() (res *http.Response, bs []byte, errs []error) {
 		return nil, nil, c.Errors
 	}
 	defer res.Body.Close()
-	bs, err = ioutil.ReadAll(res.Body)
+	bs, err = ioutil.ReadAll(io.LimitReader(res.Body, int64(2<<20))) // default 2MB change the size you want
 	if err != nil {
 		c.Errors = append(c.Errors, err)
 		return nil, nil, c.Errors

+ 3 - 2
wechat_service_api.go

@@ -13,6 +13,7 @@ import (
 	"errors"
 	"fmt"
 	"hash"
+	"io"
 	"io/ioutil"
 	"net/http"
 	"reflect"
@@ -74,8 +75,8 @@ func GetWeChatSanBoxParamSign(appId, mchId, apiKey string, bm BodyMap) (sign str
 //    返回参数bm:Notify请求的参数
 //    返回参数err:错误信息
 func ParseWeChatNotifyResultToBodyMap(req *http.Request) (bm BodyMap, err error) {
-	var bs []byte
-	if bs, err = ioutil.ReadAll(req.Body); err != nil {
+	bs, err := ioutil.ReadAll(io.LimitReader(req.Body, int64(2<<20))) // default 2MB change the size you want;
+	if err != nil {
 		return nil, fmt.Errorf("ioutil.ReadAll:%s", err.Error())
 	}
 	bm = make(BodyMap)