收到(400)错误的请求。尝试发送GCM消息时

asmgx

我正在尝试通过C#.net将消息发送到GCM服务器

我找到了一个很好的例子,我正在尝试使用

http://www.codewithasp.net/2015/11/send-message-gcm-c-sharp-single-multiple.html

问题是我收到“远程服务器返回错误:(400)错误的请求。” 每当我尝试从此行获取响应时发生错误

            WebResponse wResponse = wRequest.GetResponse();

我试图找到问题所在,将API代码从服务器更改为Android,再更改为浏览器,结果为(400)错误的请求或(401)未经授权。

不确定我在这里做错了什么,在示例中看起来非常简单明了。

非常感谢帮助

这是代码

        NotificationManager nm = new NotificationManager();
        List<string> ls = new List<string>(new string[] { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" });

        nm.SendNotification(ls, " Hi.. This is a test","Hi.. Title",1);

这是课程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Net;
using System.Text;
using System.IO;

namespace WebApplication1
{

    public class NotificationManager
    {
        private class NotificationMessage
        {
            public string Title;
            public string Message;
            public long ItemId;
        }

        public NotificationManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string SendNotification(List<string> deviceRegIds, string message, string title, long id)
        {
            try
            {
                string regIds = string.Join("\",\"", deviceRegIds);

                string AppId = "AIzaSyXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                var SenderId = "1234567890";

                NotificationMessage nm = new NotificationMessage();
                nm.Title = title;
                nm.Message = message;
                nm.ItemId = id;

                var value = new JavaScriptSerializer().Serialize(nm);
                WebRequest wRequest;
                wRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
                wRequest.Method = "post";
                wRequest.ContentType = " application/json;charset=UTF-8";
                wRequest.Headers.Add(string.Format("Authorization: key={0}", AppId));

                wRequest.Headers.Add(string.Format("Sender: id={0}", SenderId));

                string postData = "{\"collapse_key\":\"score_update\",\"time_to_live\":108,\"delay_while_idle\":true,\"data\": { \"message\" : " + "\"" + value + "\",\"time\": " + "\"" + System.DateTime.Now.ToString() + "\"},\"registration_ids\":[\"" + regIds + "\"]}";

                Byte[] bytes = Encoding.UTF8.GetBytes(postData);
                wRequest.ContentLength = bytes.Length;

                Stream stream = wRequest.GetRequestStream();
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();

                WebResponse wResponse = wRequest.GetResponse();

                stream = wResponse.GetResponseStream();

                StreamReader reader = new StreamReader(stream);

                String response = reader.ReadToEnd();

                HttpWebResponse httpResponse = (HttpWebResponse)wResponse;
                string status = httpResponse.StatusCode.ToString();

                reader.Close();
                stream.Close();
                wResponse.Close();

                if (status == "")
                {
                    return response;
                }
                else
                {
                    return "";
                }
            }
            catch( Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}

这是我获取API ID和发件人ID的方法

发件人ID

API ID

尼基尔·高尔(Nikhil gaur)

我是您引用的本网站的作者。感谢您指出错误,实际上问题出在postData字符串中。我们将值用双引号括起来(“ like this”),但这是一个json字符串,因此我们不需要这样做。只需删除postData字符串中值的双引号,它将为您工作。

所以只要改变这个

string postData = "{\"collapse_key\":\"score_update\",\"time_to_live\":108,\"delay_while_idle\":true,\"data\": { \"message\" : " + "\"" + value + "\",\"time\": " + "\"" + System.DateTime.Now.ToString() + "\"},\"registration_ids\":[\"" + regIds + "\"]}";

对此

string postData = "{\"collapse_key\":\"score_update\",\"time_to_live\":108,\"delay_while_idle\":true,\"data\": { \"message\" : " + value + ",\"time\": " + "\"" + System.DateTime.Now.ToString() + "\"},\"registration_ids\":[\"" + regIds + "\"]}";

到此为止。

我还更正了链接的帖子,以使其他访问者可能不会遇到相同的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在尝试发送POST请求时发生错误?

尝试在HTTPCLIENT -JAVA中发送POST请求时,收到400错误请求

尝试通过Twilio发送消息时遇到奇怪的错误

尝试登录时Yii2中的错误请求(#400)

Gmail API:尝试发送电子邮件时出现400错误的请求(PHP代码)

使用python发送消息时出现错误400

尝试发送嵌入内容时,我不断收到错误消息

尝试在Shopify上发布请求时,请求给出400错误

POST方法在被请求时不断发送400个错误

当我尝试通过访存与Api连接时,为什么会收到“错误请求”错误400?

尝试运行serve -s build时收到错误消息

jQuery $ .ajax通过发送POST接收到400错误请求

尝试从我的Twilio号码向我的电话号码发送消息时收到以下错误

尝试启动服务时,etcd错误拒绝发送消息

尝试使用“ http:// localhost:61961 / Token”登录到Web API。收到400错误的请求错误

尝试在Python中打破while循环时收到错误消息

尝试使用twilio发送消息时发生致命错误

尝试发送帖子并总是收到错误的请求

尝试计算 p_value 时收到错误消息

尝试发送 POST 请求时收到 404 错误

尝试执行存储过程时,我不断收到错误消息

Python在发送请求时收到超时错误

我在尝试安装 Prokka 时收到错误消息

Python Flask 服务器收到“代码 400”错误(从 Telegram-webhook 发送的 POST 请求)

当我尝试通过 REST 请求发送 DM 时,我收到了错误的身份验证数据

当我尝试更改我的 Discord 头像时,为什么会收到“400:错误请求”?

当我尝试从预定的云函数访问外部 API 时,我不断收到错误消息:错误:找不到模块“请求”

当我尝试向频道发送消息时,我收到 HTTP 未经授权的错误?

尝试链接 API 时收到“400 错误请求”