title: vb.net HTTPWebRequest Header
body: <p>I am using VB.net and I am trying to load a file using HTTPWebRequest.</p>
  
  <p>I have found that if I am requesting a file that requires a username and password I need to set that in the header of the request like this:</p>
  
  <pre><code>Digest username=admin,password=21232f297a57a5a743894a0e4a801fc3,realm=MyServer,nonce="3b010c090c0a0000c0a80157c70079babf",uri="/data.xml",cnonce="3690046e5d2f641e5c4a8bd3dcf1f541",nc=00000003,algorithm=MD5,response="8f6129b934467074b3d0b9c543f7e025",qop="auth",opaque="4e6573732041636365737320436f6e74"
  </code></pre>
  
  <p>Howerver I found that that if the username or password or even the realm, nonce, cnonce,  response, opaque is wrong then it will deny access as well.</p>
  
  <p>However when this happens and it denys access I need a way to read the header of the denyed header so I can read realm, nonce amd cnonce from it.</p>
  
  <p>When I try and catch the error I am not sure what to use.</p>
  
  <p>Is there a way to view the denyed header?</p>
  
  <p>My Code I am using looks like this:</p>
  
  <p>(it works but if the username or password etc. is incorrect it fails and I need to view the header of the failed request)</p>
  
  <pre><code>' Create a WebRequest to the remote site
  Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://192.168.0.30/data.xml")
  
  ' NB! Use the following line ONLY if the website is protected
  request.Credentials = New System.Net.NetworkCredential("admin", "admin")
  
  request.UseDefaultCredentials = False
  
  request.Accept = "*/*"
  request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
  
  request.Headers("Accept-Encoding") = "gzip, deflate"
  request.Headers("Pragma") = "no-cache"
  
  request.ContentLength = 0
  request.Method = "POST"
  
  request.ContentType = "text/xml"
  request.PreAuthenticate = True
  
  Dim authorization_type As String = "Digest"
  Dim username As String = "admin"
  Dim password As String = "21232f297a57a5a743894a0e4a801fc3"
  Dim realm As String = "MyServer"
  
  Dim nonce As String = "3b010c090c0a0000c0a80157c70079babf"
  Dim Uri As String = "/data.xml"
  Dim cnonce As String = "3690046e5d2f641e5c4a8bd3dcf1f541"
  Dim nc As String = "00000003"
  Dim algorithm As String = "MD5"
  Dim response As String = "8f6129b934467074b3d0b9c543f7e025"
  Dim qop As String = "auth"
  Dim opaque As String = "4e6573732041636365737320436f6e74"
  
  
  Dim Authorization As String = authorization_type &amp; " username=" &amp; username &amp; ",password=" &amp; password &amp; ",realm=" &amp; realm &amp; ",nonce=""" &amp; nonce &amp; """,uri=""" &amp; Uri &amp; """,cnonce=""" &amp; cnonce &amp; """,nc=" &amp; nc &amp; ",algorithm=" &amp; algorithm &amp; ",response=""" &amp; response &amp; """,qop=""" &amp; qop &amp; """,opaque=""" &amp; opaque &amp; """"
  
  request.Headers("Authorization") = Authorization.ToString
  
  ' Call the remote site, and parse the data in a response object
  Dim response1 As System.Net.HttpWebResponse = request.GetResponse()
  
  ' Check if the response is OK (status code 200)
  If response1.StatusCode = System.Net.HttpStatusCode.OK Then
  
      ' Parse the contents from the response to a stream object
      Dim stream As System.IO.Stream = response1.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)
  
      MsgBox(contents) ' used for testing
  
      ' 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, 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: " &amp; response1.StatusCode)
  
  End If
  </code></pre>