通过TCP发送字符串(VB.NET)

时间:2012-10-07 14:06:01

标签: vb.net tcpclient

我使用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 =对象引用未设置为对象的实例   Source = App_Web_4023pkvf StackTrace:          位于D:\ Documents and Settings \ sa \ My Documents \ Visual Studio中的_Default.Login()   2010 \ WebSites \ onlinenic_vb1 \ Default.aspx.vb:第51行          at _Default.Button2_Click(Object sender,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 e)          在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,String eventArgument)          在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)          在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
  的InnerException:

1 个答案:

答案 0 :(得分:0)

你应该试试这个:

 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")