我需要从VB6向一个RESTful Web服务进行GET和POST请求。最好和最简单的方法是什么?
我需要从VB6向一个RESTful Web服务进行GET和POST请求。最好和最简单的方法是什么?
你需要添加对MSXML库的引用:
Dim sUrl As String
Dim response As String
Dim xmlhttp
Set sUrl = "http://my.domain.com/service/operation/param"
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", sURL, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send()
Dim response As String = xmlhttp.responseText
Set xmlhttp = Nothing
responseText
将会是JSON而不是XML。 - Justin Niessner最近我在一个旧的遗留应用程序中需要使用此功能进行GET请求,由于被接受的答案无法编译,所以我想发布一些可工作的代码。我相信它将帮助未来使用VB6的某些不幸者;) 这是一个漂亮干净的函数。
Public Function WebRequest(url As String) As String
Dim http As MSXML2.XMLHTTP
Set http = CreateObject("MSXML2.ServerXMLHTTP")
http.Open "GET", url, False
http.Send
WebRequest = http.responseText
Set http = Nothing
End Function
这里是使用示例:
Dim result As String
Dim url As String
url = "http://my.domain.com/service/operation/param"
result = WebRequest(url)
愉快的VB6编程!:)
http://www.webservicehost.com/webserviceop?<any parameters>