所以我在 VB.NET 中编写了这个程序(很好地接受了代码并对其进行了修改),它从 POST 中获取信息。将该信息存储到一个值中。现在正在尝试解析该值,但我无法通过。无论如何,任何人都可以提供一些我可以遵循的示例代码?
注意: XML 来自 URL POST。这是一个片段:
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml
Module Module1
Public Class WebRequestPostExample
Public Shared Sub Main()
' Create a request using a URL that can receive a post.
Dim request As WebRequest = WebRequest.Create("https://quickvin.carfax.com/1 ")
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim postData As String
postData = "<carfax-request>"
postData = postData & "<license-plate>HSM2688</license-plate>"
postData = postData & "<state>PA</state>"
postData = postData & "<vin></vin>"
postData = postData & "<product-data-id>5A5AE0DA8BC016CF</product-data-id>"
postData = postData & "<location-id>CARFAX</location-id>"
postData = postData & "</carfax-request>"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
Try
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
XDocument.Load("https://quickvin.carfax.com/1") 'something keeps failing at this point
'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/request-info/")
'Loop through the nodes
For Each m_node In m_nodelist
'Get the Gender Attribute Value
Dim licenseplateAttribute = m_node.Attributes.GetNamedItem("license-plate").Value
'Get the firstName Element Value
Dim locationidValue = m_node.ChildNodes.Item(0).InnerText
'Get the lastName Element Value
Dim stateValue = m_node.ChildNodes.Item(1).InnerText
'Get the lastName Element Value
Dim vinValue = m_node.ChildNodes.Item(2).InnerText
'Write Result to the Console
Console.Write("Gender: " & licenseplateAttribute _
& " FirstName: " & locationidValue & " LastName: " _
& stateValue)
Console.Write(vbCrLf)
Next
Catch errorVariable As Exception
'Error trapping
Console.Write(errorVariable.ToString())
End Try
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
End Sub
End Class
End Module