26
26
import cn .jpush .api .common .resp .APIRequestException ;
27
27
import cn .jpush .api .common .resp .ResponseWrapper ;
28
28
29
+ /**
30
+ * The implementation has no connection pool mechanism, used origin java connection.
31
+ *
32
+ * 本实现没有连接池机制,基于 Java 原始的 HTTP 连接实现。
33
+ *
34
+ * 遇到连接超时,会自动重连指定的次数(默认为 3);如果是读取超时,则不会自动重连。
35
+ *
36
+ * 可选支持 HTTP 代理,同时支持 2 种方式:1) HTTP 头上加上 Proxy-Authorization 信息;2)全局配置 Authenticator.setDefault;
37
+ */
29
38
public class NativeHttpClient implements IHttpClient {
30
39
private static final Logger LOG = LoggerFactory .getLogger (NativeHttpClient .class );
31
40
private static final String KEYWORDS_CONNECT_TIMED_OUT = "connect timed out" ;
@@ -35,6 +44,9 @@ public class NativeHttpClient implements IHttpClient {
35
44
private String _authCode ;
36
45
private HttpProxy _proxy ;
37
46
47
+ /**
48
+ * 默认的重连次数是 3
49
+ */
38
50
public NativeHttpClient (String authCode ) {
39
51
this (authCode , DEFAULT_MAX_RETRY_TIMES , null );
40
52
}
@@ -46,6 +58,11 @@ public NativeHttpClient(String authCode, int maxRetryTimes, HttpProxy proxy) {
46
58
this ._authCode = authCode ;
47
59
this ._proxy = proxy ;
48
60
61
+ if ( null != _proxy && _proxy .isAuthenticationNeeded ()) {
62
+ Authenticator .setDefault (new SimpleProxyAuthenticator (
63
+ _proxy .getUsername (), _proxy .getPassword ()));
64
+ }
65
+
49
66
initSSL ();
50
67
}
51
68
@@ -63,6 +80,11 @@ public ResponseWrapper sendPost(String url, String content)
63
80
throws APIConnectionException , APIRequestException {
64
81
return doRequest (url , content , RequestMethod .POST );
65
82
}
83
+
84
+ public ResponseWrapper sendPut (String url , String content )
85
+ throws APIConnectionException , APIRequestException {
86
+ return doRequest (url , content , RequestMethod .PUT );
87
+ }
66
88
67
89
public ResponseWrapper doRequest (String url , String content ,
68
90
RequestMethod method ) throws APIConnectionException , APIRequestException {
@@ -108,8 +130,6 @@ private ResponseWrapper _doRequest(String url, String content,
108
130
conn = (HttpURLConnection ) aUrl .openConnection (_proxy .getNetProxy ());
109
131
if (_proxy .isAuthenticationNeeded ()) {
110
132
conn .setRequestProperty ("Proxy-Authorization" , _proxy .getProxyAuthorization ());
111
- Authenticator .setDefault (new SimpleProxyAuthenticator (
112
- _proxy .getUsername (), _proxy .getPassword ()));
113
133
}
114
134
} else {
115
135
conn = (HttpURLConnection ) aUrl .openConnection ();
@@ -124,14 +144,14 @@ private ResponseWrapper _doRequest(String url, String content,
124
144
conn .setRequestProperty ("Accept-Charset" , CHARSET );
125
145
conn .setRequestProperty ("Charset" , CHARSET );
126
146
conn .setRequestProperty ("Authorization" , _authCode );
127
-
147
+ conn .setRequestProperty ("Content-Type" , CONTENT_TYPE_JSON );
148
+
128
149
if (RequestMethod .GET == method ) {
129
150
conn .setDoOutput (false );
130
151
} else if (RequestMethod .DELETE == method ) {
131
152
conn .setDoOutput (false );
132
- } else if (RequestMethod .POST == method ) {
153
+ } else if (RequestMethod .POST == method || RequestMethod . PUT == method ) {
133
154
conn .setDoOutput (true );
134
- conn .setRequestProperty ("Content-Type" , CONTENT_TYPE_JSON );
135
155
byte [] data = content .getBytes (CHARSET );
136
156
conn .setRequestProperty ("Content-Length" , String .valueOf (data .length ));
137
157
out = conn .getOutputStream ();
@@ -141,7 +161,7 @@ private ResponseWrapper _doRequest(String url, String content,
141
161
142
162
int status = conn .getResponseCode ();
143
163
InputStream in = null ;
144
- if (status == 200 ) {
164
+ if (status / 100 == 2 ) {
145
165
in = conn .getInputStream ();
146
166
} else {
147
167
in = conn .getErrorStream ();
@@ -165,11 +185,11 @@ private ResponseWrapper _doRequest(String url, String content,
165
185
String reset = conn .getHeaderField (RATE_LIMIT_Reset );
166
186
wrapper .setRateLimit (quota , remaining , reset );
167
187
168
- if (status == 200 ) {
169
- LOG .debug ("Succeed to get response - 200 OK" );
188
+ if (status >= 200 && status < 300 ) {
189
+ LOG .debug ("Succeed to get response OK - responseCode:" + status );
170
190
LOG .debug ("Response Content - " + responseContent );
171
191
172
- } else if (status > 200 && status < 400 ) {
192
+ } else if (status >= 300 && status < 400 ) {
173
193
LOG .warn ("Normal response but unexpected - responseCode:" + status + ", responseContent:" + responseContent );
174
194
175
195
} else {
@@ -185,7 +205,7 @@ private ResponseWrapper _doRequest(String url, String content,
185
205
wrapper .setErrorObject ();
186
206
break ;
187
207
case 403 :
188
- LOG .error ("Request is forbidden! Maybe your appkey is listed in blacklist? " );
208
+ LOG .error ("Request is forbidden! Maybe your appkey is listed in blacklist or your params is invalid. " );
189
209
wrapper .setErrorObject ();
190
210
break ;
191
211
case 410 :
0 commit comments