我正在編寫一個模塊,用於使用其新的HTTP提供程序API將推送通知發送到使用APN的IOS設備。但是我在閱讀過程中遇到了一些問題從response.Its拋出以下一個無效的HTTP響應和error.The輸出是一段代碼,我已經寫了:使用其HTTP API打開與APNS的連接時發生無效的Http響應
static SSLSocketFactory getSSLSocketFactory() {
try {
String keystoreFilename = "D:/Oracle/IDCS/MFA/APNS/OMA_prereqs/iOS_prod.p12";
char[] storepass = "welcome1".toCharArray();
FileInputStream fis = new FileInputStream(
new File(keystoreFilename));
final KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(fis, storepass);
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance("SunX509");
keyManagerFactory.init(ks, storepass);
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance("SunX509");
trustManagerFactory.init((KeyStore) null);
// trustManagerFactory.init(ks);
// create ssl context
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
// setup the HTTPS context and parameters
sslContext.init(keyManagerFactory.getKeyManagers(),
trustManagerFactory.getTrustManagers(), null);
if (keyManagerFactory != null || trustManagerFactory != null) {
return SSLTunnelSocketFactory.wrap(proxy,
sslContext.getSocketFactory());
}
} catch (Exception e) {
System.out.println("Unable to create ssl socket factory");
e.printStackTrace();
}
return HttpsURLConnection.getDefaultSSLSocketFactory();
}
private static void sendSamplePushNotification() {
System.setProperty("javax.net.debug", "all");
URL url = null;
try {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
JSONObject payload = new JSONObject();
JSONObject msg = new JSONObject();
try {
msg.put("alert", "Hello APNS");
payload.put("aps", msg);
} catch (JSONException e) {
e.printStackTrace();
}
url = new URL(
"https://api.development.push.apple.com:443/3/device/00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0");
HttpsURLConnection conn = (HttpsURLConnection) url
.openConnection(proxy);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setHostnameVerifier(hv);
conn.setSSLSocketFactory(getSSLSocketFactory());
/*conn.setRequestProperty("content-length", new String(payload
.toString().getBytes()));*/
conn.setConnectTimeout(3000);
conn.connect();
OutputStream os = conn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(writer);
bw.write(payload.toString());
bw.flush();
//System.out.println("Response code : " + conn.getResponseCode());
System.out.println(conn.getResponseMessage());
InputStream is = conn.getInputStream();
................
}
我得到上執行此下列情況除外:
.............................
...............................
main, WRITE: TLSv1.2 Alert, length = 26
[Raw write]: length = 31
0000: 15 03 03 00 1A 00 00 00 00 00 00 00 04 75 ED 66 .............u.f
0010: D6 88 EC E2 B4 8F FF A2 7E CC 1C 22 FF A7 EF ..........."...
main, called closeSocket(true)
null
java.io.IOException: Invalid Http response
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1889)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1884)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1883)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1456)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at com.coding.mfa.apns.driver.APNSDriver.sendSamplePushNotification(APNSDriver.java:141)
at com.coding.mfa.apns.driver.APNSDriver.main(APNSDriver.java:38)
Caused by: java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1554)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at java.net.HttpURLConnection.getResponseMessage(HttpURLConnection.java:546)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseMessage(HttpsURLConnectionImpl.java:382)
at com.coding.mfa.apns.driver.APNSDriver.sendSamplePushNotification(APNSDriver.java:140)
... 1 more
燦有人請幫助我? SSL握手似乎很好。它在線'conn.getInputStream()'失敗'
請添加完整的例外文本。此外,如果您使用調試來確定代碼在代碼中的位置,它將有所幫助。 –
@RoyFalk我已經用完整的異常堆棧跟蹤編輯了這個問題。你能幫我解決嗎?另外一個諷刺的是,從PostMan休息客戶端,我可以成功發送推送通知請求並在我的設備上接收。 –
你有沒有考慮過普希https://relayrides.github.io/pushy/?或者可能使用源代碼作爲靈感.. – Adrian