C2DM 服务器端 C# Web 服务 Error=InvalidRegistration

发布于 2024-12-04 22:36:02 字数 4092 浏览 1 评论 0原文

我到处寻找,但没有找到我的问题的答案。让我开门见山吧。我开发了一个 Android 消息应用程序来尝试 C2DM。我的应用程序获取注册 ID,并正确显示在我的日志中。然后,我将该密钥发送到我的 C# Web 服务。

然后,C# Web 服务申请一个身份验证令牌,效果很好。到目前为止没有问题。但是,一旦我使用标头(GoogleLogin auth=[AUTH_TOKEN])发布我的正文项目(registration_id,collapse_key,data.,delay_while_idle),我得到响应:“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 帐户未正确注册。会是这样吗?或者我的代码中是否存在我遗漏的错误?

I have searched everywhere and have not found an answer to my question. Let me get straight to the point. I have developed an android messaging app for the purpose of experimenting with C2DM. My app get's the registration ID and it gets displayed in my Log correctly. I then send that key through to my C# web service.

The C# Web service then applies for an auth token, which works fine. No problem so far. But, as soon as I POST my body items (registration_id, collapse_key, data.<key>, delay_while_idle) with my header(GoogleLogin auth=[AUTH_TOKEN]) I get the response: "Error=InvalidRegistration".

There is no reason for this not to work. And yes, I have tried every solution available here in stack overflow, but remained unsuccessful. Here is my main code for my server side:

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

All I can think is that my C2DM account isn't registered correctly. Could this be it? Or are there an error in my code that I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-12-11 22:36:02

好的。我找到了解决方案。

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

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

OK. I've found the solution.

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

    ....
    ....
    ....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文