我正在尝试通过 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) Bad request 或 (401) Unauthorized。
不确定我在这里做错了什么,它在示例中看起来非常简单直接。
非常感谢帮助
代码如下
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 的方法
最佳答案
我通过 HttpWebRequest
s 发送 GCM 消息时遇到了同样的错误:
The remote server returned an error: (400) Bad Request.
StatusDescription: InvalidTokenFormat
但问题不一样。我只是有错误的网址:
"https://fcm.googleapis.com/fcm/send/" //wrong
"https://fcm.googleapis.com/fcm/send"
我花了比我愿意承认的时间更长的时间来解决这个问题。而且我想免除其他人的麻烦,避免出现这种误导性的错误消息。
关于c# - 获取 (400) 错误请求。尝试发送 GCM 消息时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38277798/