2012-11-11 86 views
1

數據我有一個應該從發送Web應用程序使用谷歌雲通訊技術在asp.net(VB.Net)開發的Android設備數據下面的代碼,但我不斷收到此錯誤: 遠程服務器返回錯誤:(401)未經授權。發送使用GCM和ASP.net

代碼如下:

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() 

任何想法我怎麼能解決這個問題。

問候

胡薩姆

回答

0

也許代替

"Authorization:Here_I_Provided_the_Server_API_Key" 

你應該嘗試

"Authorization: key=Here_I_Provided_the_Server_API_Key" 

另外,請確保您有關於應用方面正確的發送者ID,該手柄GCM請求。

+0

我還添加了另一頭我的要求作爲Demonick建議對我來說這就是:** request.Headers.Add(「發件人:ID = the_ID_of_My_Project」)**。我也改變暗淡POSTDATA作爲字符串= REGID + TextBox1.Text + collapsKey **以下昏暗POSTDATA作爲字符串= 「registration_id =」 + REGID + 「&data.message =」 + TextBox1.Text + 「&collapse_key的=」 + collapsKey ** –