0

I have a fully functional "app" I created in Microsoft Access that I use control my Philips Hue lights. They operate via a RESTful interface using JSON commands and it was pretty simple to create the code in VBA. I wanted to make a standalone Windows app though so I can run it on my computers that don't have Access.

I'm trying to use Visual Studio 2015 to make a universal app using VB.net but I'm having problems converting some of my code over. I was able to fix most of the glitches, but I can't get the winHttpReq commands to work. In my research, it sounds like they don't have a direct correlation in VB.net but none of the suggestions I found have worked.

  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)

I get the error that 'CreateObject' is not declared. It may be inaccessible due to its protection level. I'm pretty new to VB.net coding but all the recommendations for alternative posting methods don't seem to work. Any suggestions would be greatly appreciated.

1 Answers1

0

In VB.Net, use 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()
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • Thanks, that's definitely the right direction. I've been fiddling with it but can't seem to find out how to get rid of the last error codes. 'GetResponse' is not a member of 'WebRequest'. 'Current' is not declared. It may be inaccessible due to its protection level. 'Console' is not declared. It may be inaccessible due to its protection level. 'Close' is not a member of 'StreamReader'. 'Close' is not a member of 'WebResponse'. – Randy Young Nov 04 '15 at 02:23
  • You need to import the `System.Net` for the `WebRequest` and the `System.IO` for the `Stream`. – Racil Hilan Nov 04 '15 at 03:50