C2DM服务器端C#Web服务错误= InvalidRegistration

时间:2011-09-15 17:05:44

标签: c# android push android-c2dm

我到处搜索,但没有找到我的问题的答案。让我直截了当。我开发了一个Android消息传递应用程序,用于试验C2DM。我的应用程序得到了注册ID,它正确地显示在我的日志中。然后我将该密钥发送到我的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帐户未正确注册。这可能吗?或者我的代码中是否存在错误?

1 个答案:

答案 0 :(得分:0)

行。我找到了解决方案。

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);

    ....
    ....
    ....
}