修改http post

This commit is contained in:
ylywyn
2015-07-04 10:28:44 +08:00
parent eb0b869f6b
commit 11acc78870
2 changed files with 34 additions and 9 deletions
+30
View File
@@ -1,6 +1,9 @@
package jpushclient
import (
"bytes"
"io/ioutil"
"net/http"
"time"
)
@@ -39,3 +42,30 @@ func SendPostBytes(url string, content []byte, authCode string) (string, error)
return req.String()
}
func SendPostBytes2(url string, data []byte, authCode string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
req.Header.Add("Charset", CHARSET)
req.Header.Add("Authorization", authCode)
req.Header.Add("Content-Type", CONTENT_TYPE_JSON)
resp, err := client.Do(req)
if err != nil {
if resp != nil {
resp.Body.Close()
}
return "", err
}
if resp == nil {
return "", nil
}
defer resp.Body.Close()
r, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(r), nil
}
+4 -9
View File
@@ -2,7 +2,6 @@ package jpushclient
import (
"encoding/base64"
"encoding/json"
"errors"
"strings"
)
@@ -29,13 +28,8 @@ func NewPushClient(secret, appKey string) *PushClient {
return pusher
}
func (this *PushClient) Send(builder interface{}) (string, error) {
content, err := json.Marshal(builder)
if err != nil {
return "", err
}
return this.SendPushBytes(content)
func (this *PushClient) Send(data []byte) (string, error) {
return this.SendPushBytes(data)
}
func (this *PushClient) SendPushString(content string) (string, error) {
@@ -51,7 +45,8 @@ func (this *PushClient) SendPushString(content string) (string, error) {
}
func (this *PushClient) SendPushBytes(content []byte) (string, error) {
ret, err := SendPostBytes(this.BaseUrl, content, this.AuthCode)
//ret, err := SendPostBytes(this.BaseUrl, content, this.AuthCode)
ret, err := SendPostBytes2(this.BaseUrl, content, this.AuthCode)
if err != nil {
return ret, err
}