Error while connecting to APNS (https://api.development.push.apple.com/)

420 Views Asked by At

I am trying to connect to APNS (https://api.development.push.apple.com/) using HttpsUrlConnection (java code). I have downloaded the certificate from https://api.development.push.apple.com/ and added to java keystore.

CODE:

public class HttpsUrlConnectionClient {

    public static void main(String[] args) {

        {
            try {
                URL obj = new URL("https://api.development.push.apple.com/3/device/<device-token>");
                HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
                con.setRequestMethod("POST");
                con.setRequestProperty("apns-topic", "<apns-topic-name>");
                con.setRequestProperty("authorization", "bearer <apns-auth-key>");
                con.setRequestProperty("content-type", "application/json");
                con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.writeBytes("{ \"aps\" : { \"alert\" : \"Hello\" } }");
                wr.flush();
                wr.close();

                int responseCode = con.getResponseCode();
                System.out.println(responseCode);


                BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();


            } catch (Exception e) {

            }

        }
    }
}

I followed steps mentioned at https://github.com/escline/InstallCert to get the server certificate and store locally.

I am always getting responseCode as -1 (invalid http response).

Please help me as I dont understand whether this is due to certifcate or something else.

0

There are 0 best solutions below