VB.NET中的MSXML
这段代码在旧的VB6中运行良好。我在VB.NET中尝试了各种方法来实现这一点,但都没有成功。有谁能帮我编写在.NET中运行的代码吗VB.NET中的MSXML,xml,vb.net,Xml,Vb.net,这段代码在旧的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" XMLEnv
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("--------------------------------------")
不要在.NET应用程序中使用MSXML。各种
System.Xml
名称空间中的类功能更强大,更易于使用。它们也可能有更长的使用寿命
事实上,您很幸运能够使用VB.NET。它具有通过XML文本和LINQ到XML的内置XML处理功能
实际上,您的代码不包含任何XML处理。您只需使用
WebClient
或WebRequest
类即可。这就是我想到的。然后,我可以将此消息的响应放入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
你见过
System.Xml
名称空间吗?我不想在NET中使用它,我只是想找到在.NET+1中做同样事情的代码:非常好,但是你应该在流
、流阅读器
和WebResponse
实例周围使用块添加,您最好让异常传播,而不是将其作为字符串返回,不告诉任何人异常是什么。