Skip to content

Commit 7d724b3

Browse files
authored
Merge pull request xingePush#17 from xingePush/16
xingePush#16 fix bug java.io.IOException: HTTPS hostname wrong
2 parents f057eef + 9311095 commit 7d724b3

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/main/java/com/tencent/xinge/XingeApp.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
import com.tencent.xinge.api.RESTAPI_V3;
1414

15+
import javax.net.ssl.HostnameVerifier;
16+
import javax.net.ssl.HttpsURLConnection;
17+
import javax.net.ssl.SSLSession;
18+
1519
/**
1620
* 提供V3接口<br>
1721
* 1. v3中appId是String类型,v2中是long <br>
@@ -70,7 +74,7 @@ public JSONObject deviceTag(String jsonRequest) {
7074
private JSONObject callRestful(String apiAddress, String jsonRequestString) {
7175

7276
URL url;
73-
HttpURLConnection http = null;
77+
HttpsURLConnection http = null;
7478
InputStreamReader isr = null;
7579
BufferedReader br = null;
7680
String ret = "";
@@ -80,7 +84,8 @@ private JSONObject callRestful(String apiAddress, String jsonRequestString) {
8084
try {
8185
url = new URL(null, apiAddress, new com.sun.net.ssl.internal.www.protocol.https.Handler());
8286
URLConnection con = url.openConnection();
83-
http = (HttpURLConnection) con;
87+
http = (HttpsURLConnection) con;
88+
http.setHostnameVerifier(new TrustAnyHostnameVerifier());
8489
http.setRequestMethod(RESTAPI_V3.HTTP_POST);
8590
http.setDoOutput(true);
8691
http.setRequestProperty("Authorization", "Basic " + authStringEnc);
@@ -151,4 +156,11 @@ public static String stringifyError(Throwable error) {
151156
return result.toString();
152157
}
153158

159+
public class TrustAnyHostnameVerifier implements HostnameVerifier {
160+
public boolean verify(String hostname, SSLSession session) {
161+
// 直接返回true
162+
return true;
163+
}
164+
}
165+
154166
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
/**
2-
* 需要做三件事<br>
3-
* 1. 鉴权
4-
* 2. json格式检查和字段内容检查
5-
* 3. 协议转换,将json转换成jce格式
2+
* 消息体的基本元素定义
63
*/
74
package com.tencent.xinge.bean;;

0 commit comments

Comments
 (0)