2626import cn .jpush .api .common .resp .APIRequestException ;
2727import cn .jpush .api .common .resp .ResponseWrapper ;
2828
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+ */
2938public class NativeHttpClient implements IHttpClient {
3039 private static final Logger LOG = LoggerFactory .getLogger (NativeHttpClient .class );
3140 private static final String KEYWORDS_CONNECT_TIMED_OUT = "connect timed out" ;
@@ -35,6 +44,9 @@ public class NativeHttpClient implements IHttpClient {
3544 private String _authCode ;
3645 private HttpProxy _proxy ;
3746
47+ /**
48+ * 默认的重连次数是 3
49+ */
3850 public NativeHttpClient (String authCode ) {
3951 this (authCode , DEFAULT_MAX_RETRY_TIMES , null );
4052 }
@@ -46,6 +58,11 @@ public NativeHttpClient(String authCode, int maxRetryTimes, HttpProxy proxy) {
4658 this ._authCode = authCode ;
4759 this ._proxy = proxy ;
4860
61+ if ( null != _proxy && _proxy .isAuthenticationNeeded ()) {
62+ Authenticator .setDefault (new SimpleProxyAuthenticator (
63+ _proxy .getUsername (), _proxy .getPassword ()));
64+ }
65+
4966 initSSL ();
5067 }
5168
@@ -63,6 +80,11 @@ public ResponseWrapper sendPost(String url, String content)
6380 throws APIConnectionException , APIRequestException {
6481 return doRequest (url , content , RequestMethod .POST );
6582 }
83+
84+ public ResponseWrapper sendPut (String url , String content )
85+ throws APIConnectionException , APIRequestException {
86+ return doRequest (url , content , RequestMethod .PUT );
87+ }
6688
6789 public ResponseWrapper doRequest (String url , String content ,
6890 RequestMethod method ) throws APIConnectionException , APIRequestException {
@@ -108,8 +130,6 @@ private ResponseWrapper _doRequest(String url, String content,
108130 conn = (HttpURLConnection ) aUrl .openConnection (_proxy .getNetProxy ());
109131 if (_proxy .isAuthenticationNeeded ()) {
110132 conn .setRequestProperty ("Proxy-Authorization" , _proxy .getProxyAuthorization ());
111- Authenticator .setDefault (new SimpleProxyAuthenticator (
112- _proxy .getUsername (), _proxy .getPassword ()));
113133 }
114134 } else {
115135 conn = (HttpURLConnection ) aUrl .openConnection ();
@@ -124,14 +144,14 @@ private ResponseWrapper _doRequest(String url, String content,
124144 conn .setRequestProperty ("Accept-Charset" , CHARSET );
125145 conn .setRequestProperty ("Charset" , CHARSET );
126146 conn .setRequestProperty ("Authorization" , _authCode );
127-
147+ conn .setRequestProperty ("Content-Type" , CONTENT_TYPE_JSON );
148+
128149 if (RequestMethod .GET == method ) {
129150 conn .setDoOutput (false );
130151 } else if (RequestMethod .DELETE == method ) {
131152 conn .setDoOutput (false );
132- } else if (RequestMethod .POST == method ) {
153+ } else if (RequestMethod .POST == method || RequestMethod . PUT == method ) {
133154 conn .setDoOutput (true );
134- conn .setRequestProperty ("Content-Type" , CONTENT_TYPE_JSON );
135155 byte [] data = content .getBytes (CHARSET );
136156 conn .setRequestProperty ("Content-Length" , String .valueOf (data .length ));
137157 out = conn .getOutputStream ();
@@ -141,7 +161,7 @@ private ResponseWrapper _doRequest(String url, String content,
141161
142162 int status = conn .getResponseCode ();
143163 InputStream in = null ;
144- if (status == 200 ) {
164+ if (status / 100 == 2 ) {
145165 in = conn .getInputStream ();
146166 } else {
147167 in = conn .getErrorStream ();
@@ -165,11 +185,11 @@ private ResponseWrapper _doRequest(String url, String content,
165185 String reset = conn .getHeaderField (RATE_LIMIT_Reset );
166186 wrapper .setRateLimit (quota , remaining , reset );
167187
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 );
170190 LOG .debug ("Response Content - " + responseContent );
171191
172- } else if (status > 200 && status < 400 ) {
192+ } else if (status >= 300 && status < 400 ) {
173193 LOG .warn ("Normal response but unexpected - responseCode:" + status + ", responseContent:" + responseContent );
174194
175195 } else {
@@ -185,7 +205,7 @@ private ResponseWrapper _doRequest(String url, String content,
185205 wrapper .setErrorObject ();
186206 break ;
187207 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. " );
189209 wrapper .setErrorObject ();
190210 break ;
191211 case 410 :
0 commit comments