VB.net - HttpWebRequest 401 Unauthorized Error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: VB.net - HttpWebRequest 401 Unauthorized Error

  1. #1
    Join Date
    Mar 2006

    VB.net - HttpWebRequest 401 Unauthorized Error


    I am trying to connect to a XML file that is stored on a device and this XML file is password protected with a username and password.

    If I open a web browser and type in the URL to the XML it asks for a username and password (User: admin / Pass: admin) and it loads up the XML file fine.

    However, I am now trying to load this XML up in VB.net (2012) but I seem to get an error saying: The remote server returned an error: (401) Unauthorized.

    and the following line is where it stops on with the above error:

    Dim response As System.Net.HttpWebResponse = request.GetResponse()
    However I know the username and password is correct as I could load it up in the web browser.

    I am using the following VB.net code to do this:

            ' Create a WebRequest to the remote site
            Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("")
            ' 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
                ' 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
                ' If the call to the remote site fails, you'll 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
    My App.config file looks like this:
    <?xml version="1.0" encoding="utf-8" ?>
          <httpWebRequest useUnsafeHeaderParsing = "true"/>
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    Does anyone know why I am getting the 401 Unauthorized error?

  2. #2
    Join Date
    Feb 2013

    Re: VB.net - HttpWebRequest 401 Unauthorized Error

    You're passing the incorrect credentials most probably. Anything else past the Credentials part doesn't matter if you don't have the credentials right, if you're getting a 401 code.

    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  3. #3
    Join Date
    Mar 2006

    Re: VB.net - HttpWebRequest 401 Unauthorized Error


    I had to add the username, password, realm, nc, .. etc in the header as it wasn't including it when I sent the request.

    request.Headers("Authorization") = .....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center