Accessing Web Interface using VB .net

Accessing Web Interface using VB .net

dan.husted
Enthusiast Enthusiast
860 Views
1 Reply
Message 1 of 2

Accessing Web Interface using VB .net

dan.husted
Enthusiast
Enthusiast

I am attempting to automate VRED through the Web Interface as described in this Autodesk University tutorial:

http://au.autodesk.com/au-online/classes-on-demand/class-catalog/2015/vred/at10281#chapter=0

 

The tutorial uses the following python script to attach to the web interface:

import urllib
import socket

# Function to send a command to VRED and optionally return a response.
def sendVredCmd(cmd, replyNeeded):
     reply = None
     cmd = urllib.quote(cmd)
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.connect(("localhost", 8888))
     s.send("GET ")
     if replyNeeded:
          s.send("/pythoneval?value=")
     else:
          s.send("/python?value=")
     s.send(cmd)
     s.send(" HTTP/1.1\r\n\r\n")
     if replyNeeded:
          reply = s.recv(5000)
     s.close()
     return reply

 

I am trying to use this method, but access VRED using VB .net.  Below is a simplified version of the code I have tried.  Eventually I plan to have two way communication with VRED, but for now I am just attempting to send a simple print command.

 

Imports System.Text
Imports System.IO
Imports System.Net.Sockets

Public Class Main

     Private Sub GoButton_Click(sender As Object, e As EventArgs) Handles GoButton.Click

          Dim sResp As String = SendVREDCmd("print 'Test'", False) '"getActiveCameraNode().getWorldTransform()", True)

     End Sub

     Private Const sServiceSite As String = "http://localhost:8888"
     Private Const sContentType = "application/x-www-form-urlencoded"

     Private Function SendVREDCmd(sPython As String, bReply As Boolean) As String

 

          ' Create byte array
          Dim sPostData As String
          If bReply Then
               sPostData = "/pythoneval?value="
          Else
                sPostData = "/python?value="
          End If
          sPostData &= sPython
          Dim byteArray As Byte() = Encoding.ASCII.GetBytes(sPostData)

 

          ' Mothod #1 - Socket
          Dim oClient As New TcpClient(System.Net.Sockets.AddressFamily.InterNetwork)
          oClient.Connect("localhost", 8888)
          oClient.SendBufferSize = byteArray.Length
          Dim oNetStream As NetworkStream = oClient.GetStream()
          oNetStream.Write(byteArray, 0, byteArray.Length)
          oNetStream.Flush()
          oNetStream.Close()
          oClient.Close() 

 

          ' Mothod # 2 - Web Request - Post
          Dim oReqPost As Net.WebRequest = Net.WebRequest.Create(sServiceSite)
          oReqPost.Method = "POST"
          oReqPost.ContentLength = byteArray.Length
          Dim oStream As Stream = oReqPost.GetRequestStream()
          oStream.Write(byteArray, 0, byteArray.Length)
          oStream.Close()
          oStream = Nothing

 

          ' Mothod #3 - Web Request - Get
          Dim oReqGet As Net.WebRequest = Net.WebRequest.Create(sServiceSite & sPostData)
          oReqGet.Method = "GET"
          oReqGet.ContentType = sContentType
          Dim oResp As Net.WebResponse = oReqGet.GetResponse()

          Return ""

     End Function

End Class

 

All 3 of the methods failed.  The first two fail silently.  The third method actually executes the python "print" command twice (printing "Test" twice in the terminal window) and errors out giving "The underlying connection was closed: The connection was closed unexpectedly.

 

Does anybody have any experience using the Web Interface for executing python scripts?  I know this isn't a .net forum but maybe someone has tried something other than python.

 

Thanks for any help you can provide.

0 Likes
Accepted solutions (1)
861 Views
1 Reply
Reply (1)
Message 2 of 2

dan.husted
Enthusiast
Enthusiast
Accepted solution

I solved my own problem.  Revised VB .net code:

 

Imports System.Text
Imports System.IO
Imports System.Net.Sockets

Public Class Main

     Private Sub GoButton_Click(sender As Object, e As EventArgs) Handles GoButton.Click

          Dim sResp As String = SendVREDCmd("getActiveCameraNode().getWorldTransform()", True) '"print 'Test'"    

          If sResp IsNot Nothing Then Debug.Print(sResp)

     End Sub

 

     Private Const sPythonHead As String = "vred_python_result=["

     Private Function SendVREDCmd(sPython As String, bReply As Boolean) As String

 

          ' Create stream byte array
          Dim sPostData As String
          If bReply Then
                 sPostData = "/pythoneval?value="
          Else
                 sPostData = "/python?value="
          End If
          sPostData &= System.Net.WebUtility.UrlEncode(sPython)

          Dim HTTP_string As String = "GET " & sPostData & " HTTP/1.1" & vbCrLf & vbCrLf
          Dim encoding = New ASCIIEncoding()
          Dim byteArray As Byte() = encoding.GetBytes(HTTP_string)

 

          ' Upload to vred
          Dim tcp As New TcpClient("localhost", 8888)
          Dim sReply As String = Nothing
          Dim tcp_stream As NetworkStream = tcp.GetStream()
          tcp_stream.Write(byteArray, 0, byteArray.Length)
          tcp_stream.Flush()
          If bReply Then

               ' Receive return data
               ReDim byteArray(10000)
               tcp_stream.Read(byteArray, 0, 10000)
               sReply = System.Text.Encoding.ASCII.GetString(byteArray)
               sReply = sReply.Substring(0, sReply.IndexOf(Chr(&H0)))
               If sReply.Contains(sPythonHead) Then
                    sReply = sReply.Substring(sReply.IndexOf(sPythonHead) + sPythonHead.Length)
                    sReply = sReply.Substring(0, sReply.Length - 1)
               Else
                    sReply = Nothing
               End If
          End If
          tcp_stream.Close()
          tcp_stream.Dispose()
          tcp.Close()

 

          Return sReply

 

     End Function

End Class

 

Note that under the VRED Preferences for WebInterface "Enable Web Server" must be checked.

0 Likes