EDN Admin
Well-known member
Hello,
I am using HttpWebRequest / HttpWebResponse to request and get a response of a XML file using VB.net
This XML file is password protected I have worked out how to submit the username and password, but if the username and password is wrong I get a 401 error.
Just wondering if there is an error, how can I get the header of the error such as reading the realm, cn, cnonce, qop etc from the failed connection?
Based on my code below, it stops on the following line with a 401 errorim response As System.Net.HttpWebResponse = request.GetResponse()
Is there a way to view the header within my application and display a messagebox with realm, cn, cnonce, qop etc in it from the header of the failed connection?
The code I am using is as follows: Create a WebRequest to the remote site
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://192.168.0.50/data.xml")
NB! Use the following line ONLY if the website is protected
request.Credentials = New System.Net.NetworkCredential("admin", "admin")
Call the remote site, and parse the data in a response object
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Check if the response is OK (status code 200)
If response.StatusCode = System.Net.HttpStatusCode.OK Then
Parse the contents from the response to a stream object
Dim stream As System.IO.Stream = response.GetResponseStream()
Create a reader for the stream object
Dim reader As New System.IO.StreamReader(stream)
Read from the stream object using the reader, put the contents in a string
Dim contents As String = reader.ReadToEnd()
Create a new, empty XML document
Dim document As New System.Xml.XmlDocument()
Load the contents into the XML document
document.LoadXml(contents)
Now you have a XmlDocument object that contains the XML from the remote site, you can
use the objects and methods in the System.Xml namespace to read the document
Else
If the call to the remote site fails, youll have to handle this. There can be many reasons, ie. the
remote site does not respond (code 404) or your username and password were incorrect (code 401)
See the codes in the System.Net.HttpStatusCode enumerator
Throw New Exception("Could not retrieve document from the URL, response code: " & response.StatusCode)
End If
View the full article
I am using HttpWebRequest / HttpWebResponse to request and get a response of a XML file using VB.net
This XML file is password protected I have worked out how to submit the username and password, but if the username and password is wrong I get a 401 error.
Just wondering if there is an error, how can I get the header of the error such as reading the realm, cn, cnonce, qop etc from the failed connection?
Based on my code below, it stops on the following line with a 401 errorim response As System.Net.HttpWebResponse = request.GetResponse()
Is there a way to view the header within my application and display a messagebox with realm, cn, cnonce, qop etc in it from the header of the failed connection?
The code I am using is as follows: Create a WebRequest to the remote site
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://192.168.0.50/data.xml")
NB! Use the following line ONLY if the website is protected
request.Credentials = New System.Net.NetworkCredential("admin", "admin")
Call the remote site, and parse the data in a response object
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Check if the response is OK (status code 200)
If response.StatusCode = System.Net.HttpStatusCode.OK Then
Parse the contents from the response to a stream object
Dim stream As System.IO.Stream = response.GetResponseStream()
Create a reader for the stream object
Dim reader As New System.IO.StreamReader(stream)
Read from the stream object using the reader, put the contents in a string
Dim contents As String = reader.ReadToEnd()
Create a new, empty XML document
Dim document As New System.Xml.XmlDocument()
Load the contents into the XML document
document.LoadXml(contents)
Now you have a XmlDocument object that contains the XML from the remote site, you can
use the objects and methods in the System.Xml namespace to read the document
Else
If the call to the remote site fails, youll have to handle this. There can be many reasons, ie. the
remote site does not respond (code 404) or your username and password were incorrect (code 401)
See the codes in the System.Net.HttpStatusCode enumerator
Throw New Exception("Could not retrieve document from the URL, response code: " & response.StatusCode)
End If
View the full article