我有一个功能齐全的应用程序"我在Microsoft Access中创建,我使用控制我的飞利浦Hue灯。它们使用JSON命令通过RESTful接口进行操作,在VBA中创建代码非常简单。我想创建一个独立的Windows应用程序,所以我可以在没有Access的计算机上运行它。
我尝试使用Visual Studio 2015使用VB.net制作通用应用,但我在转换部分代码时遇到了问题。我能够解决大部分故障,但我无法使winHttpReq命令工作。在我的研究中,听起来他们在VB.net中没有直接相关性,但我找到的建议都没有奏效。
Dim Result As String
Dim MyURL As String, postData As String, strQuote As String
Dim winHttpReq As Object
winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
'Create address and lamp
MyURL = "http://" & IP.Text & "/api/" & Hex.Text & "/lights/" & "1" & "/state"
postData = Code.Text
winHttpReq.Open("PUT", MyURL, False)
winHttpReq.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
winHttpReq.Send(postData)
我收到错误' CreateObject'没有宣布。由于其保护级别,它可能无法访问。我对VB.net编码很陌生,但是对替代发布方法的所有建议似乎都不起作用。任何建议将不胜感激。
答案 0 :(得分:0)
在VB.Net中,使用documentation:
'Create address and lamp
MyURL = "http://" & IP.Text & "/api/" & Hex.Text & "/lights/" & "1" & "/state"
Dim request As WebRequest = WebRequest.Create(MyURL)
' 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.
Dim dataStream As Stream = 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)
' Clean up the streams and the response.
reader.Close()
response.Close()