diff --git a/httpclient.go b/httpclient.go index f771a21..3245981 100644 --- a/httpclient.go +++ b/httpclient.go @@ -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 +} diff --git a/pushclient.go b/pushclient.go index e1d1e11..f1a78a4 100644 --- a/pushclient.go +++ b/pushclient.go @@ -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 }