Skip to content

Commit fa63442

Browse files
authored
Merge pull request xingePush#18 from xingePush/16
xingePush#16 fix bug java.io.IOException: HTTPS hostname wrong
2 parents 7d724b3 + 3a83a32 commit fa63442

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.tencent.xinge;
22

33
import java.io.*;
4-
import java.net.HttpURLConnection;
54
import java.net.MalformedURLException;
65
import java.net.URL;
7-
import java.net.URLConnection;
86
import java.nio.charset.StandardCharsets;
97

8+
import com.tencent.xinge.push.app.PushAppRequest;
109
import org.apache.commons.codec.binary.Base64;
1110
import org.json.JSONObject;
1211

@@ -62,6 +61,17 @@ public JSONObject pushApp(String jsonRequest) {
6261

6362
}
6463

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+
6575
/**
6676
*
6777
* @param jsonRequest jsonRequest
@@ -74,41 +84,40 @@ public JSONObject deviceTag(String jsonRequest) {
7484
private JSONObject callRestful(String apiAddress, String jsonRequestString) {
7585

7686
URL url;
77-
HttpsURLConnection http = null;
87+
HttpsURLConnection https = null;
7888
InputStreamReader isr = null;
7989
BufferedReader br = null;
8090
String ret = "";
8191
String temp;
8292
JSONObject jsonRet = null;
8393

8494
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);
92101

93102
byte[] out = jsonRequestString.getBytes(StandardCharsets.UTF_8);
94103
int length = out.length;
95104

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");
98107

99-
http.connect();
108+
https.connect();
100109
try {
101-
OutputStream os = http.getOutputStream();
110+
OutputStream os = https.getOutputStream();
102111
os.write(out);
103112

104113
} catch (Exception e) {
105114

106115
}
107116

108117

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());
112121
br = new BufferedReader(isr);
113122
while ((temp = br.readLine()) != null) {
114123
ret += temp;
@@ -140,8 +149,8 @@ private JSONObject callRestful(String apiAddress, String jsonRequestString) {
140149
// ignore
141150
}
142151
}
143-
if (http != null) {
144-
http.disconnect();
152+
if (https != null) {
153+
https.disconnect();
145154
}
146155
}
147156

src/main/java/com/tencent/xinge/push/app/PushAppRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ public class PushAppRequest {
8484
private String push_id;
8585

8686

87+
private final static ObjectMapper mapper = new ObjectMapper();
88+
8789
@Override
8890
public String toString() {
89-
ObjectMapper mapper = new ObjectMapper();
9091
String repoStr = null;
9192
try {
9293
repoStr = mapper.writeValueAsString(this);

src/test/java/com/tencent/xinge/bean/BrowserTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.codehaus.jackson.map.ObjectMapper;
44
import org.junit.Test;
5-
import sun.jvm.hotspot.utilities.Assert;
65

76
import static org.junit.Assert.*;
87

src/test/java/com/tencent/xinge/push/app/PushAccountListAndroidTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.tencent.xinge.BaseXgTest;
44
import com.tencent.xinge.bean.*;
5-
import com.tencent.xinge.push.app.PushAppRequest;
65
import org.junit.Test;
76

87
import java.util.ArrayList;

0 commit comments

Comments
 (0)