问题描述
我已成功在Android应用程序的gcm服务器上注册设备ID。
现在我想从C#winform应用程序发送通知。我正在使用以下代码,但我收到的错误称为
远程服务器返回错误:(401)未经授权。
请帮助我
这是我的代码
我尝试过:
string GoogleAppID =AIzaSyB8vlCeU_MIiDVXFYl0uOxZNt9xZF7jHQ4;
var SENDER_ID =851345542956;
var value = message;
WebRequest tRequest;
tRequest = WebRequest.Create(https://android.googleapis.com/gcm/send);
tRequest.Method =post;
tRequest.ContentType =application / x-www-form-urlencoded; charset = UTF-8;
tRequest.Headers.Add(string.Format(授权) :key = {0},GoogleAppID));
tRequest.Headers.Add(string.Format(Sender:id = {0},SENDER_ID));
string postData =collapse_key = score_update& time_to_live = 108& delay_while_idle = 1& data .message =+ value +& data.time =+ System.DateTime.Now.ToString()+®istration_id=+ deviceId +;
Console.WriteLine(postData );
Byte [] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
流dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray,0,byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServe r = tReader.ReadToEnd();
tReader.Close();
dataStream.Close( );
tResponse.Close();
返回sResponseFromServer;
I have successful to register device id on gcm server from android app.
Now i want send notification from C# winform application.I am using this following code but i am getting error called
The remote server returned an error: (401) Unauthorized.
Please help me
This is my code
What I have tried:
string GoogleAppID = "AIzaSyB8vlCeU_MIiDVXFYl0uOxZNt9xZF7jHQ4";
var SENDER_ID = "851345542956";
var value = message;
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
推荐答案
这篇关于将C#winform应用程序的通知推送到android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!