473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to handle empty LoginResponse if read by XMLTextReader

I am using VB.Net. My program is to connect to a remote IPAddress. Once, it
verifies the login information it should display the SessionID and enable
some button . I appreciate your help and thanku in advance

When I run the pgm , I get the error:
Can't parse login information. Object Reference not set to an instance of an
object. I have added the Try-catch in all my functions. In ParseLoginResponse
function, it catches the above exception.

I am inclding all the functions I am using below:

Private Function SendXmlRequest(ByRef xml As String)

Try
Dim xmpRequest As HttpWebRequest = WebRequest.Create(XmpUrl)

' Instantiate an XmlDocument object to avoid NullReference
exception

Dim xmlDoc As XmlDocument

xmlDoc = New System.Xml.XmlDocument

'System.Xml.XmlDocument(xmldoc = New System.Xml.XmlDocument)
'Dim xmlDoc As XmlDocument = Nothing
'Dim xmlReader As XmlTextReader

'xmlReader = Nothing
xmpRequest.Method = "POST"
xmpRequest.KeepAlive = True

xmpRequest.ContentType = "text/xml"

Dim writer As StreamWriter

writer = New StreamWriter(xmpRequest.GetRequestStream())

writer.Write(xml)

writer.Close()
'Send the request and get the response

Dim HttpWResponse As HttpWebResponse =
CType(xmpRequest.GetResponse(), HttpWebResponse)
'Console.WriteLine("before sending status")
'Get the status and the headers
Dim iStatCode As Integer = HttpWResponse.StatusCode

Dim sStatus As String = iStatCode.ToString()
Console.WriteLine("Status: {0} {1}", sStatus,
HttpWResponse.StatusDescription.ToString())

Console.WriteLine("Request Headers:")

Console.WriteLine(xmpRequest)

Console.WriteLine("Response Headers:")

Console.WriteLine(HttpWResponse.Headers.ToString() )
'feb15

If Not HttpWResponse Is Nothing Then

Dim XmlRead = New
XmlTextReader(HttpWResponse.GetResponseStream())

'Dim xmlDoc As XmlDocument

'xmlDoc = New XmlDocument

'Dim xmlDoc = New XmlDocument

xmlDoc.Load(XmlRead)

Return xmlDoc
Else

Console.WriteLine("try again")

End If
'Get the response stream

Dim strm As Stream = HttpWResponse.GetResponseStream()
'Read the Response stream

Dim encode As Encoding =
System.Text.Encoding.GetEncoding("iso-8859-1")

Dim sr As New StreamReader(strm, encode)

Console.WriteLine("Response: {0}", sr.ReadToEnd())

sr.Close()

'new feb13

'Dim doc As New XmlDocument

'doc.Load(HttpWResponse.GetResponseStream())

'doc.Save(Console.Out)

'end

HttpWResponse.Close()
'Read the response stream

'Dim sr As StreamReader = New StreamReader(strm)

'Dim sText As String = sr.ReadToEnd()

'Console.WriteLine("Response: {0}", sText)

'???Close the stream

'strm.Close()

'Clean up

'xmpRequest = Nothing

'HttpWResponse = Nothing

'MyCredentialCache = Nothing

'myCred = Nothing

'strm = Nothing

'sr = Nothing

'Feb14

'If sStatus = 200 Then

'The Http Request/Response was successful, now we need to check
the XML that was returned for errors

'If Not RequestHasErrrors(xmpRequest.responseRext) Then

'The overall request was successful so begin to parse the
response XML

'SendXmlRequest = xmpRequest.responseXml

'End If

'Else

'End If
'End If

'Else

'Call MsgBox("There was an error. ReadyState = " &
xmpRequest.readyState)

'End If

Catch ex As NullReferenceException

MsgBox("Can't Request page" & vbCrLf & ex.Message)

End Try

End Function

Private Function RequestHasErrors(ByRef xml As String) As Boolean

' Check for the high level error Xml in any response before

' trying to parse the results.

If InStr(xml, "<Response><Error>") >= 1 Then

RequestHasErrors = True

End If

End Function

Private Function ParseLoginResponse(ByRef loginDom As XmlDocument) As
Boolean

' See if the login was successful. If so, get the SessionID.

' Otherwise, log the error.

Try
ParseLoginResponse = False

' Setup the Dom to use XPath queries and also setup

' an Xml namespace prefix for use in the XPath queries.

'Call loginDom.setProperty("SelectionLanguage", "XPath")

'loginDom.setProperty("SelectionNamespaces",
"xmlns:ns='urn:schemas-tms:LoginResponse'")

Dim filterDoc As New XmlDocument

'Feb14

'Instantiate an XmlNamespaceManager object

Dim nsmgr As XmlNamespaceManager

nsmgr = New XmlNamespaceManager(filterDoc.NameTable)

'Add the namespaces used to the XmlNamespaceManager.
nsmgr.AddNamespace("ns", "urn:schemas -tms:LoginResponse")

Dim node As XmlNode

node = loginDom.SelectSingleNode("ns:LoginResponse/ns:Status")

'node = New XmlDocument

If node.Value = "Success" Then

' The login was 100% successful so get the SessionID.

'new Feb 14

'If node Is Nothing Then

'for testing

Console.WriteLine("Login successful: " & node.Value)

node =
loginDom.SelectSingleNode("ns:LoginResponse/ns:SessionID")

'Dim resultImport As XmlNode

'resultImport = filterDoc.ImportNode(node, True)

'filterDoc.AppendChild(resultImport)

'Return True

'g_SessionID = node.Value

ParseLoginResponse = True

'Console.WriteLine("Login successful: " & node.Value)

'Feb14

Else

'The login was not 100% successful

node =
loginDom.SelectSingleNode("ns:LoginResponse/ns:SystemMessage")

Console.WriteLine("Login unsuccessful: " & node.InnerText)



End If

Catch ex As Exception

MsgBox("Can't parse login information" & vbCrLf & ex.Message)

End Try

'Feb13()

'Return True

'Else

' The login was not 100% successful.

'node = loginDom.SelectSingleNode("ns:LoginResponse/ns:SystemMessage")

'Console.WriteLine("Login unsuccessful: " & node.text)

'End If

'Feb13

'loginDom = Nothing

'node = Nothing

End Function



End Class


Feb 17 '06 #1
0 1393

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
2
by: Mitch | last post by:
I have some simple HTML I'm trying to read with the XMLTextReader. As in the MSDS examples, I set up a loop to read each XML node: while (reader.Read()) { switch (reader.NodeType) { case...
3
by: juli jul | last post by:
Hello, How can I read xml file but ignore the empty lines in it : is there some kind of function in C# that can read everything except for the empty lines? Thanks a lot! *** Sent via...
4
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.