BC30188:预期宣言;新的NetworkCredential VB.NET

时间:2011-11-09 15:16:38

标签: asp.net vb.net apple-push-notifications urbanairship.com

经过多年的一次编程(经典ASP已经12年),我开始做更多的编程,结果我自学了.net。

我正在尝试使用UrbanAirship的API通过Apple的推送通知服务器(APNS)发送测试推送通知。我找到了这个示例代码,但是很难实现它。

我收到错误:BC30188:预期声明。以下是此特定行的代码:

req.Credentials = New NetworkCredential("username", "password")

这是我的整个代码:

pushvb.aspx

<%@ Page Language="VB" AutoEventWireup="false" src="pushvb_bg.aspx.vb" Inherits="UrbanAirship.uacode" %>

<% Response.write(UrbanAirship.uacode.testing) %>

pushvb_bg.aspx.vb

Imports System   
Imports System.Net  
Imports System.Text  
Imports System.IO  
Imports System.WinForms  

Namespace UrbanAirship  
public partial Class uacode  
    Inherits System.Web.UI.Page  
    Public Const testing As String = "testing..."  
    Dim req As WebRequest = WebRequest.Create("https://go.urbanairship.com/api/push/")  
    Dim postData As String = "{""aps"": {""badge"": ""+1"", ""alert"": ""pushvb"",""sound"": ""default"",""device_tokens"": ""token""}}"  
    req.Credentials = New NetworkCredential("username", "password")  
    req.Method = "POST"  
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)  
    req.ContentType = "application/json"  
    req.ContentLength = byteArray.Length  
    Dim dataStream As Stream = req.GetRequestStream()  
    dataStream.Write(byteArray, 0, byteArray.Length)  
    dataStream.Close()  
    Dim resp As WebResponse = req.GetResponse() 
    dataStream = resp.GetResponseStream()  
    Dim reader As New StreamReader(dataStream)  
    Dim responseFromServer As String = reader.ReadToEnd()  
    Console.WriteLine(responseFromServer)  
    reader.Close()  
    dataStream.Close()  
    req.Close()  
end Class  
end Namespace  

Imports System Imports System.Net Imports System.Text Imports System.IO Imports System.WinForms Namespace UrbanAirship public partial Class uacode Inherits System.Web.UI.Page Public Const testing As String = "testing..." Dim req As WebRequest = WebRequest.Create("https://go.urbanairship.com/api/push/") Dim postData As String = "{""aps"": {""badge"": ""+1"", ""alert"": ""pushvb"",""sound"": ""default"",""device_tokens"": ""token""}}" req.Credentials = New NetworkCredential("username", "password") req.Method = "POST" Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) req.ContentType = "application/json" req.ContentLength = byteArray.Length Dim dataStream As Stream = req.GetRequestStream() dataStream.Write(byteArray, 0, byteArray.Length) dataStream.Close() Dim resp As WebResponse = req.GetResponse() dataStream = resp.GetResponseStream() Dim reader As New StreamReader(dataStream) Dim responseFromServer As String = reader.ReadToEnd() Console.WriteLine(responseFromServer) reader.Close() dataStream.Close() req.Close() end Class end Namespace

非常感谢任何帮助。非常感谢您抽出时间来查看。

1 个答案:

答案 0 :(得分:2)

嗯......答案很简单。在方法之外,只允许声明。您需要在您的类中创建一个方法,该方法可以从您那里调用,或者例如页面加载事件。

dim和new的行被接受为声明,Public Const也是声明。

req.Credentials = New NetworkCredential("username", "password")是代码中的第一行,它不是声明,而是赋值,因此它会显示在错误窗口中。