2

我正在使用 VB.net,我正在尝试使用 HTTPWebRequest 加载文件。

我发现如果我请求一个需要用户名和密码的文件,我需要在请求的标头中设置它,如下所示:

Digest username=admin,password=21232f297a57a5a743894a0e4a801fc3,realm=MyServer,nonce="3b010c090c0a0000c0a80157c70079babf",uri="/data.xml",cnonce="3690046e5d2f641e5c4a8bd3dcf1f541",nc=00000003,algorithm=MD5,response="8f6129b934467074b3d0b9c543f7e025",qop="auth",opaque="4e6573732041636365737320436f6e74"

但是我发现如果用户名或密码甚至领域、nonce、cnonce、response、opaque 错误,那么它也会拒绝访问。

但是,当这种情况发生并且它拒绝访问时,我需要一种方法来读取被拒绝的标头的标头,以便我可以从中读取领域、随机数和随机数。

当我尝试捕获错误时,我不确定该使用什么。

有没有办法查看被拒绝的标题?

我正在使用的代码如下所示:

(它有效,但如果用户名或密码等不正确,它会失败,我需要查看失败请求的标头)

' 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 & " username=" & username & ",password=" & password & ",realm=" & realm & ",nonce=""" & nonce & """,uri=""" & Uri & """,cnonce=""" & cnonce & """,nc=" & nc & ",algorithm=" & algorithm & ",response=""" & response & """,qop=""" & qop & """,opaque=""" & opaque & """"

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: " & response1.StatusCode)

End If
4

0 回答 0