我使用 TCP 客户端成功连接到 onlinenic API,但在尝试登录时遇到错误。
下面是我的代码(用户名和密码是由 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(data, 0, data.Length)
System.NullReferenceException 未被用户代码处理
Message=Object 引用未设置为对象的实例。
Source=App_Web_4023pkvf StackTrace:在 D:\Documents and Settings\sa\My Documents\Visual Studio 2010\WebSites\onlinenic_vb1\Default.aspx.vb 中的 _Default.Login() 处:_Default.Button2_Click 的第 51 行(对象发件人,EventArgs e ) 在 D:\Documents and Settings\sa\My Documents\Visual Studio 2010\WebSites\onlinenic_vb1\Default.aspx.vb:line 243 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web .UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在 System.Web.UI.Page 的字符串 eventArgument)。ProcessRequestMain(布尔型 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)
内部异常: