我正在尝试使用 java 程序构建一个简单的推送通知,以将消息广播到所有 Android 设备,但我总是收到 Connectionexception,我不知道为什么......
URL url = new URL("https://go.urbanairship.com/api/push/broadcast/");
// Transmit the request document
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("connect 1");
connection.setDoOutput(true);
String auth = "*****************" + ":" + "******************";
//connection.setRequestProperty("Authorization","Basic " + Base64.encode(auth.getBytes()));
connection.setRequestProperty("Authorization", "Basic " + auth);
System.out.println("connect 2");
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setConnectTimeout(12000);
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Content-Length", "" + Integer.toString(bytes.length));
connection.connect();
System.out.println("connect 3");
//Send request
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.write(bytes);
wr.flush ();
wr.close ();
System.out.println("connect 5");
//Get Response
String responseHeader = connection.getHeaderField(0);
System.out.println("response header " + responseHeader);
输出:
connect 1
connect 2
Exception in thread "main" java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)
at sun.net.NetworkClient.doConnect(NetworkClient.java:158)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:133)
at TestProject.main(TestProject.java:34)