简体   繁体   English

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

[英]BC30188: Declaration expected ; New NetworkCredential VB.NET

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. 经过多年的一点编程(经典ASP 12年),我开始做更多的编程,结果我自学了.net。

I'm attempting to use UrbanAirship's API to send a test push notification through Apple's Push Notification Server (APNS). 我正在尝试使用UrbanAirship的API通过Apple的推送通知服务器(APNS)发送测试推送通知。 I found this sample code, but am having a hard time implementing it. 我找到了这个示例代码,但我很难实现它。

I'm receiving the error: BC30188: Declaration expected. 我收到错误:BC30188:预期声明。 Here is the code on this particular line: 以下是此特定行的代码:

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

Here is my entire code: 这是我的整个代码:

pushvb.aspx pushvb.aspx

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

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

pushvb_bg.aspx.vb 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 eg the Page Load Event. 您需要在类中创建一个从您调用的方法,或者例如页面加载事件。

Lines with dim and a new are accepted as a declaration, Public Const is also a Declaration. 具有暗淡和新的行被接受为声明,Public Const也是声明。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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