开发者

C2DM server side C# web service Error=InvalidRegistration

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 = (HttpWebRespon开发者_如何转开发se)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?


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

    ....
    ....
    ....
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜