1

此代码在良好的旧 VB6 中运行良好。我已经尝试了各种方法在 VB.NET 中执行此操作,但无法使其正常工作。任何人都可以帮我编写在 .NET 中工作的代码吗?

Dim objHTTP As New MSXML2.XMLHTTP

Dim strReturn As String
Dim objReturn As New MSXML2.DOMDocument


Dim url As String
Dim XMLEnvelope As String


url = "http://zzzzzdummy.com"

XMLEnvelope = vbNull



objHTTP.open("post", url, False, "", "")  '

Debug.Print(Err.Number)

objHTTP.setRequestHeader("Content-Type", "text/xml")
objHTTP.setRequestHeader("Content-length", Len(XMLEnvelope))
Debug.Print("------------Send Envelope-------------")
Debug.Print(XMLEnvelope)
Debug.Print("--------------------------------------")
objHTTP.send(XMLEnvelope)

strReturn = objHTTP.responseText
objReturn.loadXML(strReturn)
Debug.Print("----------Response Envelope-----------")
Debug.Print(strReturn)
Debug.Print("--------------------------------------")
4

2 回答 2

2

这就是我想出的。然后我可以将来自此的响应放入 XML 返回并解析它。

Function WRequest(ByVal URL As String, ByVal method As String, ByVal POSTdata As String) As String
     Dim responseData As String = ""

     Try
         Dim hwrequest As Net.HttpWebRequest = Net.WebRequest.Create(URL)
         hwrequest.Accept = "*/*"
         hwrequest.AllowAutoRedirect = True
         hwrequest.UserAgent = "http_requester/0.1"
         hwrequest.Timeout = 60000
         hwrequest.Method = method

         If hwrequest.Method = "POST" Then
             hwrequest.ContentType = "application/x-www-form-urlencoded"

             Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
             Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
             hwrequest.ContentLength = postByteArray.Length

             Dim postStream As IO.Stream = hwrequest.GetRequestStream()
             postStream.Write(postByteArray, 0, postByteArray.Length)
             postStream.Close()

         End If

         Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
         If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
             Dim responseStream As IO.StreamReader =  New IO.StreamReader(hwresponse.GetResponseStream())
             responseData = responseStream.ReadToEnd()
         End If

         hwresponse.Close()
     Catch e As Exception
         responseData = "An error occurred: " & e.Message
     End Try
     Return responseData

 End Function
于 2012-05-08T17:05:54.947 回答
1

不要在 .NET 应用程序中使用 MSXML。各种System.Xml命名空间中的类更强大,更易于使用。它们也可能具有更长的使用寿命。

您实际上很幸运能够使用 VB.NET。它通过 XML 文字和 LINQ to XML 内置了对 XML 的处理。


实际上,您的代码不包括对 XML 的任何处理。您应该简单地使用WebClientorWebRequest类。

于 2012-05-08T00:47:55.627 回答