我使用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