我使用TCP客戶端成功連接到onlinenic API,但在嘗試登錄時面臨錯誤。通過TCP(VB.NET)發送字符串
下面是我的代碼(用戶名和密碼演示如被OnlineNIC公司提供)
Imports System
Imports System.Net.Sockets
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Configuration
Imports System.Xml.Linq
Imports System.Xml
Partial Public Class _Default
Inherits System.Web.UI.Page
Private client As New TcpClient()
Private stream As NetworkStream
Private PortNo As Integer = 30009
Private testIP As String = "218.5.81.149"
Private testID As String = "135610"
Private testPassword As String = "654123"
Private Function Connect() As Boolean
client.Connect(testIP, PortNo)
stream = client.GetStream()
Dim responseData As String = ""
Dim data As [Byte]() = New [Byte](255) {}
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = Encoding.ASCII.GetString(data, 0, bytes)
Return responseData.Contains("Your Connection with API Server is Successful")
End Function
Private Function Login() As Boolean
Dim HashedPass As String = CreateMd5Hash(testPassword)
Dim guid__1 As Guid = Guid.NewGuid()
Dim chksum As String = CreateMd5Hash(testID + HashedPass + guid__1.ToString() + "login")
Dim sb As New StringBuilder()
sb.Append("<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>")
sb.Append("<request>")
sb.Append("<category>client</category>")
sb.Append("<action>Login</action>")
sb.Append("<params>")
sb.Append("<param name=""clid"">" + testID + "</param>")
sb.Append("</params>")
sb.Append("<cltrid>" + guid__1.ToString() + "</cltrid>")
sb.Append("<chksum>" + chksum + "</chksum>")
sb.Append("</request>")
Dim responseData As [String] = [String].Empty
Dim data As [Byte]()
data = System.Text.Encoding.ASCII.GetBytes(sb.ToString())
stream.Write(data, 0, data.Length)
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
bytes = stream.Read(data, 0, data.Length)
responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Return responseData.Contains("Command completed successfully")
End Function
當我嘗試使用我有以下錯誤上述login()函數來連接:
對象未將引用設置爲對象的實例。
Line 49: Dim data As [Byte]()
Line 50: data = System.Text.Encoding.ASCII.GetBytes(sb.ToString())
Line 51: stream.Write(data, 0, data.Length)
Line 52: Dim bytes As Int32 = stream.Read(data, 0, data.Length)
Line 53: responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
該錯誤消息正在逐漸在這條線:stream.Write(數據1,0,data.Length)
System.NullReferenceException是由用戶代碼未被設置
消息= Object引用未處理到一個對象的一個實例。
源= App_Web_4023pkvf堆棧跟蹤:在d 在_Default.Login():\ Documents和Settings \ SA \我的文檔\的Visual Studio 2010 \網站已\ onlinenic_vb1 \ Default.aspx.vb:線51 在_Default.Button2_Click(對象發件人,EventArgs e)在D:\ Documents and Settings \ sa \ My Documents \ Visual Studio 2010 \ WebSites \ onlinenic_vb1 \ Default.aspx.vb:行243 在System.Web.UI.WebControls.Button.OnClick( EventArgs的) 在System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串eventArgument) 在System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument) 在系統。 Web.UI.Page.RaisePostBackEvent(IPostBackEventHand LER sourceControl,字符串eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection中POSTDATA) 在System.Web.UI.Page.ProcessRequestMain(布爾includeStagesBeforeAsyncPoint,布爾includeStagesAfterAsyncPoint)
的InnerException:
在什麼行是拋出的異常? – pilotcam
第51行:stream.Write(data,0,data.Length) – Dan
參考API正在使用的是:https://www.onlinenic.com/cp_english/template_api/download/API_EN_Version_2.0.pdf – Dan