我有一个带有ASP.NET后端的安卓应用程序。我已经获取了该手机的registration_id以及Google为应用服务器提供的auth token,用于执行推送。
当我向C2DM发出HTTP POST请求,以便手机接收到消息时,我不断收到401未经授权的错误。以下是我在.NET中进行请求的方式:
任何帮助都将不胜感激。
当我向C2DM发出HTTP POST请求,以便手机接收到消息时,我不断收到401未经授权的错误。以下是我在.NET中进行请求的方式:
WebRequest myRequest = WebRequest.Create("https://android.apis.google.com/c2dm/send");
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Method = "POST";
myRequest.Headers.Add("Authorization", "GoogleLogin auth=" + authId);
// buiold the post string
StringBuilder myPost = new StringBuilder();
myPost.AppendFormat("registration_id={0}", regId);
myPost.AppendFormat("&data.payload={0}", msg);
myPost.AppendFormat("&collapse_key={0}", colKey);
// write the post-string as a byte array
byte[] myData = ASCIIEncoding.ASCII.GetBytes(myPost.ToString());
myRequest.ContentLength = myData.Length;
Stream myStream = myRequest.GetRequestStream();
myStream.Write(myData, 0, myData.Length);
myStream.Close();
// Do the actual request and read the response stream
WebResponse myResponse = myRequest.GetResponse();
Stream myResponseStream = myResponse.GetResponseStream();
StreamReader myResponseReader = new StreamReader(myResponseStream);
string strResponse = myResponseReader.ReadToEnd();
myResponseReader.Close();
myResponseStream.Close();
任何帮助都将不胜感激。