1
1
package com .tencent .xinge ;
2
2
3
3
import java .io .*;
4
- import java .net .HttpURLConnection ;
5
4
import java .net .MalformedURLException ;
6
5
import java .net .URL ;
7
- import java .net .URLConnection ;
8
6
import java .nio .charset .StandardCharsets ;
9
7
8
+ import com .tencent .xinge .push .app .PushAppRequest ;
10
9
import org .apache .commons .codec .binary .Base64 ;
11
10
import org .json .JSONObject ;
12
11
@@ -62,6 +61,17 @@ public JSONObject pushApp(String jsonRequest) {
62
61
63
62
}
64
63
64
+ /**
65
+ *
66
+ * @param pushAppRequest PushAppRequest
67
+ * @return 通用基础返回值,是所有请求的响应中都会包含的字段
68
+ */
69
+ public JSONObject pushApp (PushAppRequest pushAppRequest ) {
70
+ String jsonRequest = pushAppRequest .toString ();
71
+ return pushApp (jsonRequest );
72
+ }
73
+
74
+
65
75
/**
66
76
*
67
77
* @param jsonRequest jsonRequest
@@ -74,41 +84,40 @@ public JSONObject deviceTag(String jsonRequest) {
74
84
private JSONObject callRestful (String apiAddress , String jsonRequestString ) {
75
85
76
86
URL url ;
77
- HttpsURLConnection http = null ;
87
+ HttpsURLConnection https = null ;
78
88
InputStreamReader isr = null ;
79
89
BufferedReader br = null ;
80
90
String ret = "" ;
81
91
String temp ;
82
92
JSONObject jsonRet = null ;
83
93
84
94
try {
85
- url = new URL (null , apiAddress , new com .sun .net .ssl .internal .www .protocol .https .Handler ());
86
- URLConnection con = url .openConnection ();
87
- http = (HttpsURLConnection ) con ;
88
- http .setHostnameVerifier (new TrustAnyHostnameVerifier ());
89
- http .setRequestMethod (RESTAPI_V3 .HTTP_POST );
90
- http .setDoOutput (true );
91
- http .setRequestProperty ("Authorization" , "Basic " + authStringEnc );
95
+ url = new URL (null , apiAddress , new sun .net .www .protocol .https .Handler ());
96
+ https = (HttpsURLConnection ) url .openConnection ();
97
+ https .setHostnameVerifier (new TrustAnyHostnameVerifier ());
98
+ https .setRequestMethod (RESTAPI_V3 .HTTP_POST );
99
+ https .setDoOutput (true );
100
+ https .setRequestProperty ("Authorization" , "Basic " + authStringEnc );
92
101
93
102
byte [] out = jsonRequestString .getBytes (StandardCharsets .UTF_8 );
94
103
int length = out .length ;
95
104
96
- http .setFixedLengthStreamingMode (length );
97
- http .setRequestProperty ("Content-Type" , "application/json; charset=UTF-8" );
105
+ https .setFixedLengthStreamingMode (length );
106
+ https .setRequestProperty ("Content-Type" , "application/json; charset=UTF-8" );
98
107
99
- http .connect ();
108
+ https .connect ();
100
109
try {
101
- OutputStream os = http .getOutputStream ();
110
+ OutputStream os = https .getOutputStream ();
102
111
os .write (out );
103
112
104
113
} catch (Exception e ) {
105
114
106
115
}
107
116
108
117
109
- http .getOutputStream ().flush ();
110
- http .getOutputStream ().close ();
111
- isr = new InputStreamReader (http .getInputStream ());
118
+ https .getOutputStream ().flush ();
119
+ https .getOutputStream ().close ();
120
+ isr = new InputStreamReader (https .getInputStream ());
112
121
br = new BufferedReader (isr );
113
122
while ((temp = br .readLine ()) != null ) {
114
123
ret += temp ;
@@ -140,8 +149,8 @@ private JSONObject callRestful(String apiAddress, String jsonRequestString) {
140
149
// ignore
141
150
}
142
151
}
143
- if (http != null ) {
144
- http .disconnect ();
152
+ if (https != null ) {
153
+ https .disconnect ();
145
154
}
146
155
}
147
156
0 commit comments