【问题标题】:send string through TCP (VB.NET)通过 TCP (VB.NET) 发送字符串
【发布时间】:2013-03-11 22:39:42
【问题描述】:

我使用 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=对象引用未设置为对象的实例。
来源=App_Web_4023pkvf StackTrace: 在 D:\Documents and Settings\sa\My Documents\Visual Studio 中的 _Default.Login() 2010\WebSites\onlinenic_vb1\Default.aspx.vb: 第 51 行 在 D:\Documents and Settings\sa\My Documents\Visual Studio 中的 _Default.Button2_Click(Object sender, EventArgs e) 2010\WebSites\onlinenic_vb1\Default.aspx.vb:243 行 在 System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在 System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 eventArgument) 在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)
内部异常:

【问题讨论】:

  • 在哪一行抛出异常?
  • 第 51 行:stream.Write(data, 0, data.Length)
  • 看起来“流”此时为空。你在 Login() 之前运行 Connect() 吗?
  • 是的,我使用 connect() 成功连接,然后尝试 login()。我从其他 C# 代码转换了这段代码。

标签: vb.net tcpclient


【解决方案1】:

你应该试试这个:

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

【讨论】:

    猜你喜欢
    • 2016-09-02
    • 2013-02-27
    • 2013-06-28
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2016-10-11
    • 2019-10-02
    相关资源
    最近更新 更多