mirror of
https://github.com/wahyd4/jpush-api-go-client.git
synced 2026-08-09 04:46:43 +10:00
修改http post
This commit is contained in:
@@ -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
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user