简体   繁体   中英

Send data using GCM and ASP.net

I have the following code that supposed to send data from web application developed in asp.net (VB.Net) to android device using Google Cloud Messaging Technology but I keep getting this error: The remote server returned an error: (401) Unauthorized.

the code is as following:

Dim regID As String = "REG_ID_ that i got when I developed the Clint side of this technology"
Dim request As WebRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.Headers.Add("Authorization:Here_I_Provided_the_Server_API_Key")
Dim collapsKey = Guid.NewGuid.ToString("N")
Dim postdata As String = regID + TextBox1.Text + collapsKey
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata)
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim resposne As WebResponse = request.GetResponse
Dim dataresponse As Stream = resposne.GetResponseStream
Dim reader As New StreamReader(dataresponse)
Dim sResponseFromServer As String = reader.ReadToEnd
Label1.Text = sResponseFromServer
reader.Close()
dataresponse.Close()
resposne.Close()

any Idea how can I fix this issue.

regards

Husam

Maybe instead of

"Authorization:Here_I_Provided_the_Server_API_Key"

you should try

"Authorization: key=Here_I_Provided_the_Server_API_Key"

Also make sure you have the correct sender ID on the application side, that handles GCM requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM