2015-11-02 69 views
0

我有一个在Microsoft Access中创建的全功能“应用程序”,我使用控制Philips Hue灯。它们使用JSON命令通过RESTful接口进行操作,在VBA中创建代码非常简单。我想制作一个独立的Windows应用程序,但是我可以在没有Access的计算机上运行它。在VisualStudio 2015中将Access VBA转换为Visual Basic

我想使用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

退房System.Web命名空间的新对于.NET 4.5+ – rheitzman

回答

0

在VB.Net,使用WebRequest

'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() 
+0

谢谢,这绝对是正确的方向。我一直在摆弄它,但似乎无法找到如何摆脱最后的错误代码。 'GetResponse'不是'WebRequest'的成员。 \t \t '当前'未被声明。由于其保护级别,它可能无法访问。 '控制台'未被声明。由于其保护级别,它可能无法访问。 “关闭”不是“StreamReader”的成员。 \t'关闭'不是'WebResponse'的成员。 –

+0

您需要为'WebRequest'导入'System.Net'并为'Stream'导入'System.IO'。 –

相关问题