在我的ASP.NET(VB)项目中,使用XML
从远程服务器获取TcpClient
响应时遇到错误。错误是Thread was being aborted.
我面临错误的代码如下所示。
Private Function GetCMSAPIXMLResp(ByVal strReqXml As String, ByVal strAPIIpAddr As String, ByVal strAPIPort As String) As String
Dim strRespXml As String = String.Empty
Try
objClientSocket = New TcpClient()
objClientSocket.Connect(strAPIIpAddr, strAPIPort)
If objClientSocket.Connected = True Then
objNetStrm = objClientSocket.GetStream()
Dim btOutStrm() As Byte = System.Text.Encoding.ASCII.GetBytes(strReqXml)
objNetStrm.Write(btOutStrm, 0, btOutStrm.Length)
objNetStrm.Flush()
If objNetStrm.CanRead Then
Dim lngBuffSize As Long = CLng(objClientSocket.ReceiveBufferSize)
Dim btInStream(lngBuffSize) As Byte
strbldRespXml = New StringBuilder()
Dim iNoBytesRead As Integer = 0
Do
iNoBytesRead = objNetStrm.Read(btInStream, 0, btInStream.Length)
strbldRespXml.AppendFormat("{0}", System.Text.Encoding.ASCII.GetString(btInStream, 0, iNoBytesRead))
Loop While objNetStrm.DataAvailable
strRespXml = Convert.ToString(strbldRespXml)
strRespXml = strRespXml.Replace(vbCrLf, vbNullString)
Else
lblError.Text = "Unable to read"
End If
Else
lblError.Text = "Unable to connect with remote server"
End If
Catch ex As Exception
Throw
Finally
objClientSocket = Nothing
objNetStrm = Nothing
strbldRespXml = Nothing
End Try
Return strRespXml
End Function
实际上,我只是想知道错误在什么时候得到以及为什么。
注意:端口打开已完成,当我ping
和telnet
到远程服务器时,获得正响应。