0

I am trying to request a XML file Using VB.net and load it in my application.

I have been able to use a JavaScript in the past which uses XMLHttpRequest but now I am trying to create a application that will allow me to request a XML file and parse it.

In the past I have used the following JavaScript code to request and show the reply in a textarea:

<html>
<body>
<button onclick="send_command()">GET</button>

<textarea id="test1" name="test1" cols="90" rows="30">
XML file will be displayed here
</textarea><br>

<script type="text/javascript">

    function send_command()
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("test1").value=xmlhttp.responseText;
            }
        }

        var url = "http://192.168.0.50/my_xml_file.xml";

        xmlhttp.open("POST",url,false,"admin","admin");
        xmlhttp.send();
    }

</script>

</body>
</html>

But now I am trying to do the same thing but using VB.net (2012) as a windows Application.

After doing a quick Google on this, I have found the following code:

' Create a WebRequest to the remote site
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://192.168.0.50/my_xml_file.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, 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

However when I run the code above in VB.net I get an error:

enter image description here

I can't seem to work out why this error is happening.

Does anyone know what the error above means or better still is the VB.net code above going to do what I am trying to do?

(My XML file requires a username and password to view the XML)

4

1 回答 1

0

一种可能的解决方案是将其放入您的 web.config 文件中:

<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>

看看是否可以解决您的错误。

于 2013-01-30T09:08:20.937 回答