After years of doing a little programming on the side (Classic ASP for 12 years), I’m starting to do a lot more programming, and as a result am teaching myself .net.
I’m attempting to use UrbanAirship’s API to send a test push notification through Apple’s Push Notification Server (APNS). I found this sample code, but am having a hard time implementing it.
I’m receiving the error: BC30188: Declaration expected. Here is the code on this particular line:
req.Credentials = New NetworkCredential("username", "password")
Here is my entire code:
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
Any help would be greatly appreciated. Thanks so much for taking the time to look over.
Well… the answer is quite simple. Outside of Methods only declarations are allowed. You need to create a method in your Class which gets called from you, or is e.g. the Page Load Event.
Lines with dim and a new are accepted as a declaration, Public Const is also a Declaration.
The line
req.Credentials = New NetworkCredential("username", "password")is the first line in the code which is not a declaration, but an assignment, therfore it is shown in your Error Window.