开发者

HTTP Post in C2DM giving SocketTimeoutException

I made an app. for Android which uses the C2DM service from Google. I made a server simulator from some tutorials and it works fine. My problem is, I tried to build a Java Servlet. From the Android device it receives fine the message and saves the Registration ID, but when I try to send a https POST request to the Google C2DM Server it always gets a SocketTimeoutException : Timeout while fetching: https://android.clients.google.com/c2dm/send. I don't get why this is happening when the same works on the Andr开发者_开发百科oid device. Here is the code:

//The AuthToken from Google Client Login

 String auth_key = TOKEN;    
 StringBuilder postDataBuilder = new StringBuilder();

 //some parameters to pass, I've checked and it's correct, it's working
 //with Fiddler
 postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(REGISTRATION_ID);
 postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
 postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, UTF8));

 byte[] postData = postDataBuilder.toString().getBytes(UTF8);
 URL url = new URL("https://android.clients.google.com/c2dm/send");

 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 conn.setDoOutput(true);
 conn.setUseCaches(false);

 conn.setRequestMethod("POST");
 conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");

conn.setRequestProperty("Content-Length",Integer.toString(postData.length));
 conn.setRequestProperty("Authorization", "GoogleLogin auth="+auth_key);

 OutputStream out = conn.getOutputStream();
 out.write(postData);
 out.close();

 int responseCode = conn.getResponseCode();
 //here comes the error processing, but I can't reach it, because of
 //the exception.



 if (responseCode == 401 || responseCode == 403) {

//....
}

Thanks for your help :).


The first obvious thing to check is - if you have thought of this I apologise - are you behind a proxy server e.g. a company firewall? If so a timeout is exactly the symptom I'd expect with the above code. (This catches me out all the time!)

With the latter half of your code (from the HttpURLConnection declaration on), unmodified, I see a timeout; on my system (behind a company firewall), with two changes I get a 200 OK back:

  • addition of a proxy object passed to the HttpUrlConnection factory as follows:

    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("...", 8080)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

  • accepting the C2DM server's certificate that wasn't trusted by my JVM. For test purposes I overrode the default hostname verifier and TrustManager as described in Trusting all certificates using HttpClient over HTTPS . For production you should look at a more secure solution.

Another thing I spotted; it doesn't seem to matter but http://code.google.com/android/c2dm/index.html#push says to post to https://android.apis.google.com/c2dm/send, not android.clients.google.com - just something to be aware of that might break in future.


I faced same problem and

I had tried :

URL url = new URL("http://android.apis.google.com/c2dm/send");

instead of :

URL url = new URL("https://android.apis.google.com/c2dm/send");

it worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜