EDN Admin
Well-known member
Hello,
I am trying to request and download a XML file.
I seem to have a problem where it tries to get the XML file but it comes back saying:The remote server returned an error: (401) Unauthorized.
The above error stops on this lineim response As System.Net.HttpWebResponse = request.GetResponse()
If I open IE and request the XML file it asks for a username and password. If I enter in my username and password for this file (admin/admin) it then loads my XML file fine.
I cant work out why my VB.net application wont accept my username and password, is there anything I am missing from my code below: 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 IfMy App.config file looks like this (not sure if anything is missing):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
If I use a sniffer program and then use Internet Explorer to request the file, the header of the request looks like below (not sure if that helps find out what I am missing in my code):Send: Return Code: 0x00000000 GET /data.xml HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: 192.168.0.50
Connection: Keep-Alive
Authorization: Digest
username="admin",realm="MyServer",nonce="3b010c090c0a0000c0a80157c7006f4110",uri="/data.xml",cnonce="1448c12057879b0a9002a574430681bb",nc=0000001d,algorithm=MD5,response="54ba42fdb1879d6583f1de9c26170531",qop="auth",opaque="4e6573732041636365737320436f6e74"
I hope someone can tell me where and what I am doing wrong as I have been tring for many hours and cant work it out.
View the full article
I am trying to request and download a XML file.
I seem to have a problem where it tries to get the XML file but it comes back saying:The remote server returned an error: (401) Unauthorized.
The above error stops on this lineim response As System.Net.HttpWebResponse = request.GetResponse()
If I open IE and request the XML file it asks for a username and password. If I enter in my username and password for this file (admin/admin) it then loads my XML file fine.
I cant work out why my VB.net application wont accept my username and password, is there anything I am missing from my code below: 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 IfMy App.config file looks like this (not sure if anything is missing):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
If I use a sniffer program and then use Internet Explorer to request the file, the header of the request looks like below (not sure if that helps find out what I am missing in my code):Send: Return Code: 0x00000000 GET /data.xml HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: 192.168.0.50
Connection: Keep-Alive
Authorization: Digest
username="admin",realm="MyServer",nonce="3b010c090c0a0000c0a80157c7006f4110",uri="/data.xml",cnonce="1448c12057879b0a9002a574430681bb",nc=0000001d,algorithm=MD5,response="54ba42fdb1879d6583f1de9c26170531",qop="auth",opaque="4e6573732041636365737320436f6e74"
I hope someone can tell me where and what I am doing wrong as I have been tring for many hours and cant work it out.
View the full article