[英]remote server returned an error (401) unauthorized
Written code to get the notification on android device. 编写代码以在Android设备上获取通知。 In this added the device id and registration id and application id of that android application but i run this page getting this error.... The remote server returned an error: (401) Unauthorized.
在此添加了该Android应用程序的设备ID,注册ID和应用程序ID,但我运行此页面时收到此错误...。远程服务器返回错误:(401)未经授权。
private void AndroidPush()
{
string regId = "[the regid]";
// applicationID means google Api key
var applicationID = "[the key]";
//ProjectID (from API Console- google code)
var SENDER_ID = "[the id]";
var value = Text1.Text; //message text box
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}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
tRequest.UseDefaultCredentials = true;
tRequest.PreAuthenticate = true;
tRequest.Credentials = CredentialCache.DefaultCredentials;
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=" + regId + "";
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(); //Get response from GCM server.
Label3.Text = sResponseFromServer; //Assigning GCM response to Label text
tReader.Close();
dataStream.Close();
tResponse.Close();
}
You can't connect to remote server if you don't enter Username and Password 如果不输入用户名和密码,则无法连接到远程服务器
Check This Answer might be helpful. 选中此答案可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.