5
5
import java .net .HttpURLConnection ;
6
6
import java .net .MalformedURLException ;
7
7
import java .net .URL ;
8
+ import java .security .KeyManagementException ;
9
+ import java .security .NoSuchAlgorithmException ;
8
10
9
11
import com .tencent .xinge .push .app .PushAppRequest ;
10
12
import org .apache .commons .codec .binary .Base64 ;
11
13
import org .json .JSONObject ;
12
14
13
15
import com .tencent .xinge .api .RESTAPI_V3 ;
14
16
15
- import javax .net .ssl .HostnameVerifier ;
16
- import javax .net .ssl .HttpsURLConnection ;
17
- import javax .net .ssl .SSLSession ;
17
+ import javax .net .ssl .*;
18
18
19
19
/**
20
20
* 提供V3接口<br>
@@ -29,6 +29,7 @@ public class XingeApp {
29
29
30
30
private String authString = null ;
31
31
private String authStringEnc = null ;
32
+ X509TrustManager trustManager = new DefaultX509TrustManager ();
32
33
33
34
/**
34
35
* HTTP Header Authorization 的值:Basic base64_auth_string<br>
@@ -104,8 +105,9 @@ private synchronized JSONObject callRestful(String apiAddress, String jsonReques
104
105
https .setReadTimeout (10000 );
105
106
https .setRequestProperty ("Content-Type" , "application/json" );
106
107
https .setRequestProperty ("Authorization" , "Basic " + authStringEnc );
107
- https .setRequestProperty ("Connection" , "Keep-Alive " );
108
+ https .setRequestProperty ("Connection" , "Keep-Alive" );
108
109
110
+ https .setSSLSocketFactory (this .getSSLSocketFactory ());
109
111
110
112
byte [] out = jsonRequestString .getBytes (Charsets .UTF_8 );
111
113
int length = out .length ;
@@ -152,6 +154,14 @@ private synchronized JSONObject callRestful(String apiAddress, String jsonReques
152
154
jsonRet .put ("ret_code" , 10101 );
153
155
jsonRet .put ("err_msg" , stringifyError (e ));
154
156
157
+ } catch (NoSuchAlgorithmException e ) {
158
+ jsonRet = new JSONObject ();
159
+ jsonRet .put ("ret_code" , 10102 );
160
+ jsonRet .put ("err_msg" , stringifyError (e ));
161
+ } catch (KeyManagementException e ) {
162
+ jsonRet = new JSONObject ();
163
+ jsonRet .put ("ret_code" , 10103 );
164
+ jsonRet .put ("err_msg" , stringifyError (e ));
155
165
} finally {
156
166
if (br != null ) {
157
167
try {
@@ -183,6 +193,22 @@ public static String stringifyError(Throwable error) {
183
193
return result .toString ();
184
194
}
185
195
196
+ private SSLSocketFactory getSSLSocketFactory () throws KeyManagementException , NoSuchAlgorithmException {
197
+ TrustManager [] tm = {this .trustManager };
198
+ SSLContext sslContext = SSLContext .getInstance ("TLSv1" );
199
+ sslContext .init (null , tm , new java .security .SecureRandom ());
200
+ SSLSocketFactory ssf = sslContext .getSocketFactory ();
201
+ return ssf ;
202
+ }
203
+
204
+ /**
205
+ * 设置证书信任管理器
206
+ * @param trustManager
207
+ */
208
+ public void setTrustManager (X509TrustManager trustManager ) {
209
+ this .trustManager = trustManager ;
210
+ }
211
+
186
212
public class TrustAnyHostnameVerifier implements HostnameVerifier {
187
213
public boolean verify (String hostname , SSLSession session ) {
188
214
// 直接返回true
0 commit comments