修改了 http 包为 goreq
diff --git a/webservice.go b/webservice.go
index ead20d5..47d750d 100644
--- a/webservice.go
+++ b/webservice.go
@@ -1,7 +1,6 @@
package swservice
import (
- "bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/tls"
@@ -9,7 +8,6 @@
"encoding/json"
"errors"
"fmt"
- log "github.com/Sirupsen/logrus"
"io/ioutil"
"net"
"net/http"
@@ -18,6 +16,9 @@
"strings"
"sync"
"time"
+
+ log "github.com/Sirupsen/logrus"
+ "github.com/franela/goreq"
)
type WebSession struct {
@@ -146,10 +147,7 @@
return &transport
}
-func (w *WebSession) DoPost(uri string, param map[string]string) (*http.Response, error) {
- transport := w.httpConnectionPool.Get().(*http.Transport)
- defer w.httpConnectionPool.Put(transport)
- client := &http.Client{Transport: transport, Timeout: time.Duration(3) * time.Second}
+func (w *WebSession) DoPost(uri string, param map[string]string) (*goreq.Response, error) {
param["app_id"] = w.AppId
param["term_id"] = w.TermId
param["sign_method"] = "HMAC"
@@ -163,8 +161,13 @@
if err != nil {
return nil, err
}
- var r *http.Response
- r, err = client.Post(full_url, "application/json", bytes.NewReader(data))
+ var r *goreq.Response
+ r, err = goreq.Request{
+ Uri: full_url,
+ Method: "POST",
+ ContentType: "application/json",
+ Body: data,
+ Timeout: time.Duration(3) * time.Second}.Do()
if err != nil || r.StatusCode != 200 {
log.Errorf("Status=%v, err=%v", r, err)
}
@@ -296,9 +299,6 @@
func (w *WebSession) CallService2(path string, params map[string]interface{}, timeout int,
sign_field ...string) (response *ServiceResponse, err error) {
- transport := w.httpConnectionPool.Get().(*http.Transport)
- defer w.httpConnectionPool.Put(transport)
- client := http.Client{Transport: transport, Timeout: time.Duration(timeout) * time.Second}
err = nil
params["app_id"] = w.AppId
params["term_id"] = w.TermId
@@ -323,12 +323,14 @@
full_url := w.BaseUrl + path
log.Debugf("CallService: %v\n", full_url)
- var r *http.Response
- r, err = client.Post(full_url, "application/x-www-form-urlencoded",
- bytes.NewReader([]byte(vl.Encode())))
- if r != nil {
- defer r.Body.Close()
- }
+ var r *goreq.Response
+ r, err = goreq.Request{
+ Uri: full_url,
+ Accept: "application/json",
+ ContentType: "application/x-www-form-urlencoded",
+ Method: "POST",
+ Timeout: time.Duration(timeout) * time.Second,
+ Body: vl.Encode()}.Do()
if err != nil {
log.Errorf("Status=%v, err=%v", r, err)
return
@@ -338,6 +340,9 @@
err = errors.New(fmt.Sprintf("Request Error, StatusCode : %v", r.StatusCode))
return
}
+ if r.Body != nil {
+ defer r.Body.Close()
+ }
body, err := ioutil.ReadAll(r.Body)
var s interface{}
err = json.Unmarshal(body, &s)