Skip to content

Commit 27c64db

Browse files
authored
Merge pull request xingePush#24 from xingePush/19
xingePush#19 https 域名访问偶发错误
2 parents 6a4ee46 + 7a90ebe commit 27c64db

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Charsets;
44
import java.io.*;
5+
import java.net.HttpURLConnection;
56
import java.net.MalformedURLException;
67
import java.net.URL;
78

@@ -81,7 +82,7 @@ public JSONObject deviceTag(String jsonRequest) {
8182
return callRestful(RESTAPI_V3.RESTAPI_TAG, jsonRequest);
8283
}
8384

84-
private JSONObject callRestful(String apiAddress, String jsonRequestString) {
85+
private synchronized JSONObject callRestful(String apiAddress, String jsonRequestString) {
8586

8687
URL url;
8788
HttpsURLConnection https = null;
@@ -97,15 +98,22 @@ private JSONObject callRestful(String apiAddress, String jsonRequestString) {
9798
https.setHostnameVerifier(new TrustAnyHostnameVerifier());
9899
https.setRequestMethod(RESTAPI_V3.HTTP_POST);
99100
https.setDoOutput(true);
101+
https.setDoInput(true);
102+
https.setUseCaches(false);
103+
https.setConnectTimeout(10000);
104+
https.setReadTimeout(10000);
105+
https.setRequestProperty("Content-Type", "application/json");
100106
https.setRequestProperty("Authorization", "Basic " + authStringEnc);
107+
https.setRequestProperty("Connection", "Keep-Alive ");
101108

102-
byte[] out = jsonRequestString.getBytes(Charsets.UTF_8);
109+
110+
byte[] out = jsonRequestString.getBytes(Charsets.UTF_8);
103111
int length = out.length;
104112

105113
https.setFixedLengthStreamingMode(length);
106-
https.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
107114

108115
https.connect();
116+
109117
try {
110118
DataOutputStream os = new DataOutputStream(https.getOutputStream());
111119
os.write(out);
@@ -118,7 +126,16 @@ private JSONObject callRestful(String apiAddress, String jsonRequestString) {
118126

119127
https.getOutputStream().flush();
120128
https.getOutputStream().close();
121-
isr = new InputStreamReader(https.getInputStream());
129+
130+
InputStream in = null;
131+
if (https.getResponseCode() >= 400) {
132+
in = https.getErrorStream();
133+
} else {
134+
in = https.getInputStream();
135+
}
136+
isr = new InputStreamReader(in);
137+
138+
isr = new InputStreamReader(in);
122139
br = new BufferedReader(isr);
123140
while ((temp = br.readLine()) != null) {
124141
ret += temp;

src/test/java/com/tencent/xinge/BaseXgTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class BaseXgTest {
77
public XingeApp xingeAndroid = null;
88
public XingeApp xingeIOS = null;
99
private String appIdAndroid = "d617a675b62d0";
10-
private String secretKeyAndroid = "9e05364c6d56da943783e61da091e8e5";
10+
private String secretKeyAndroid = "b5da4c1faf95ee9409cd33792b02f5d2";
1111
private String appIdIOS = "f248587cdc955";
1212
private String secretKeyIOS = "c6bdff7314d4367c18bacc4d30c95dcf";
1313

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import com.tencent.xinge.BaseXgTest;
44
import com.tencent.xinge.bean.*;
5+
import com.tencent.xinge.XingeApp;
56
import com.tencent.xinge.push.app.PushAppRequest;
67
import org.junit.Test;
78

89
import java.util.ArrayList;
910

1011
public class PushSingleAccountAndroidTest extends BaseXgTest {
11-
12-
12+
private String appIdAndroid = "d617a675b62d0";
13+
private String secretKeyAndroid = "b5da4c1faf95ee9409cd33792b02f5d2";
1314
@Test
1415
public void testPushSingleAccountAndroid() {
1516

@@ -45,7 +46,7 @@ public void testPushSingleAccountAndroid() {
4546
pushAppRequest.setAudience_type(AudienceType.account);
4647
pushAppRequest.setSeq(123);
4748
pushAppRequest.setPush_id("0");
48-
pushAppRequest.setSend_time("1111111");
49+
pushAppRequest.setSend_time("2018-10-09 17:24:20");
4950
pushAppRequest.setStat_tag("test");
5051
pushAppRequest.setMessage(message);
5152

@@ -54,7 +55,14 @@ public void testPushSingleAccountAndroid() {
5455
pushAppRequest.setAccount_list(accountList);
5556

5657
System.out.println(pushAppRequest.toString());
57-
System.out.println(xingeAndroid.pushApp(pushAppRequest.toString()));
58+
for (int i=0; i< 5; i++) {
59+
// try {
60+
// Thread.sleep(5000);
61+
// } catch (Exception e) {
62+
//
63+
// }
64+
System.out.println(xingeAndroid.pushApp(pushAppRequest.toString()));
65+
}
5866

5967
}
6068
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public void testPushTagAndroid() {
5252

5353

5454
System.out.println(pushAppRequest.toString());
55-
System.out.println(xingeAndroid.pushApp(pushAppRequest.toString()));
55+
for (int i=0; i< 3; i++) {
56+
System.out.println(xingeAndroid.pushApp(pushAppRequest.toString()));
57+
58+
}
59+
60+
5661

5762
}
5863
}

0 commit comments

Comments
 (0)