C2DM服务器端c# web服务错误=InvalidRegistration

本文关键字:错误 InvalidRegistration 服务 web 服务器端 C2DM | 更新日期: 2023-09-27 18:08:56

我到处找了,还没有找到我问题的答案。让我开门见山吧。我开发了一个android消息应用程序,用于实验C2DM。我的app获得了注册ID,并正确地显示在我的Log中。然后,我将该密钥发送到我的c# web服务。

c# Web服务然后申请一个验证令牌,它工作得很好。到目前为止还没有问题。但是,只要我发布我的主体项目(registration_id, collapse_key, data.<key>, delay_while_idle)与我的标题(GoogleLogin auth=[AUTH_TOKEN]),我得到response: "Error=InvalidRegistration".

没有理由不工作。是的,我已经尝试了所有可用的堆栈溢出解决方案,但仍然没有成功。以下是我的服务器端主代码:

 WebRequest theRequest;
            HttpWebResponse theResponse;
            ArrayList theQueryData;
            theRequest = WebRequest.Create("https://www.google.com/accounts/ClientLogin");                
            theRequest.Method = "POST";
            theQueryData = new ArrayList();
            String [] test = new String[5];
            test[0] = "accountType=HOSTED_OR_GOOGLE";
            test[1] = "Email=XXXXXXXXXXXXXXXXX";
            test[2] = "Passwd=XXXXXXXXXXXXXXXX";
            test[3] = "Source=Domokun";
            test[4] = "service=ac2dm";
            // Set the encoding type
            theRequest.ContentType = "application/x-www-form-urlencoded";
            // Build a string containing all the parameters
            string Parameters = String.Join("&", (String[])test);
            theRequest.ContentLength = Parameters.Length;
            // We write the parameters into the request
            StreamWriter sw = new StreamWriter(theRequest.GetRequestStream());
            sw.Write(Parameters);
            sw.Close();
            // Execute the query
            theResponse = (HttpWebResponse)theRequest.GetResponse();
            StreamReader sr = new StreamReader(theResponse.GetResponseStream());
            String value = sr.ReadToEnd();
            String token = ParseForAuthTokenKey(value);
            String value2 = "";
            if (value != null)
            {
                WebRequest theRequest2;
                HttpWebResponse theResponse2;
                ArrayList theQueryData2;
                theRequest2 = WebRequest.Create("http://android.clients.google.com/c2dm/send");
                theRequest2.Method = "POST";

                theQueryData2 = new ArrayList();
                String[] test2 = new String[4];
                test[0] = "registration_id=" + registerid;
                test[1] = "collapse_key=0";
                test[2] = "data.payload=Jannik was hier"; 
                test[3] = "delay_while_idle=0";
                // Set the encoding type
                theRequest2.ContentType = "application/x-www-form-urlencoded";

                // Build a string containing all the parameters
                string Parameters2 = String.Join("&", (String[])test2);
                theRequest2.ContentLength = Parameters2.Length;
                theRequest2.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + token);

                // We write the parameters into the request
                StreamWriter sw2 = new StreamWriter(theRequest2.GetRequestStream());
                sw2.Write(Parameters2);
                sw2.Close();
                // Execute the query
                theResponse2 = (HttpWebResponse)theRequest2.GetResponse();
                StreamReader sr2= new StreamReader(theResponse2.GetResponseStream());
                value2 = sr2.ReadToEnd();

 public static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true;
    }
    private static string ParseForAuthTokenKey(string webResponse)
    {
        string tokenKey = String.Empty;
        if (webResponse.Contains(AuthTokenHeader))
        {
            tokenKey = webResponse.Substring(webResponse.IndexOf(AuthTokenHeader) + AuthTokenHeader.Length);
            if (tokenKey.Contains(Environment.NewLine))
            {
                tokenKey.Substring(0, tokenKey.IndexOf(Environment.NewLine));
            }
        }
        return tokenKey.Trim();
    }

我能想到的就是我的C2DM账户没有正确注册。会是这样吗?还是我的代码中有我遗漏的错误?

C2DM服务器端c# web服务错误=InvalidRegistration

OK。我找到解决办法了。

string requestBody = string.Format("registration_id={0}&collapse_key{1}&data.key=value",
                                    HttpUtility.UrlEncode(registrationId), "collapse");
string responseBody = null;
WebHeaderCollection requestHeaders = new WebHeaderCollection();
WebHeaderCollection responseHeaders = null;
requestHeaders.Add(HttpRequestHeader.Authorization, string.Format("GoogleLogin auth={0}", authToken));

httpClient.DoPostWithHeaders(c2dmPushUrl,
                  requestBody,
                  "application/x-www-form-urlencoded",
                  out responseBody,
                  out responseHeaders,
                  requestHeaders);
public bool DoPostWithHeaders(string url, 
                              string requestBody, 
                              string contextType,
                          out string responseBody,
                          out WebHeaderCollection responseHeaders,
                              WebHeaderCollection requestHeaders = null)
{
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    // FIRST SET REQUEST HEADERS
    httpWebRequest.Headers = requestHeaders;
    httpWebRequest.Method = "POST";
    // THEN SET CONTENT TYPE - THE ORDER IS IMPORTANT
    httpWebRequest.ContentType = contextType; 
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = encoding.GetBytes(requestBody);
    httpWebRequest.ContentLength = data.Length;
    stream = httpWebRequest.GetRequestStream();
    stream.Write(data, 0, data.Length);
    ....
    ....
    ....
}