Announcement

Collapse
No announcement yet.

Error Running Script... works with tenscripting but not from HSTouch or an event

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Error Running Script... works with tenscripting but not from HSTouch or an event

    I have a script command that calls a routine to make a web post request. When I run the script using tenscripting it works, when I call the same script from HSTouch or an event, I get the following error:

    Error 3
    Running script AVControl.vb :Exception has been thrown by the target of an invocation.->Does entry point RokuCmd exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Scheduler.clsRunVBNetScript.ExecuteScript()
    Below is the script... the error is triggered by the GetRequestStream() call that is shown in red.
    Public Sub WebPost(ByVal command As String)

    Dim url As String = command
    Dim mypost As String = ""
    Dim tempCookies As New CookieContainer
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse

    request = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
    request.ContentType = "text/html; charset=UTF-8"
    request.ContentLength = mypost.Length
    request.Method = "POST"
    request.KeepAlive = True
    request.AllowAutoRedirect = False
    request.CookieContainer = tempCookies
    request.Referer = url
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"

    MyWriteLog(1, "Test:", "before getrequeststream")

    Dim requeststream As Stream = request.GetRequestStream()
    MyWriteLog(1, "Test:", "after getrequeststream")

    Dim postbytes As Byte() = Encoding.ASCII.GetBytes(mypost)
    requeststream.Write(postbytes, 0, postbytes.Length)
    requeststream.Close()
    response = DirectCast(request.GetResponse(), HttpWebResponse)

    Dim myreader As New StreamReader(response.GetResponseStream())

    responsetxt = myreader.ReadToEnd

    End Sub

    Any help would be greatly appreciated... Thanks, Mike

    #2
    From the error transcript, it appears that the answer to

    Does entry point RokuCmd exist in script?

    is "no" ... ?

    Comment


      #3
      RokuCmd does exist and is actually the script that calls the WebPost routine.

      Comment


        #4
        I don't use HS Touch, but I've read on the forum that scripts called from it, expect an array to be passed to it. If it's called from an event, then a string needs to be passed. Without seeing the other script that contains RokuCmd, it's hard to help troubleshoot...
        HS 3.0.0.548: 1990 Devices 1172 Events
        Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

        Comment


          #5
          I just tried calling the WebPost script directly rather than calling it through the RokuCmd routine so that I could eliminate it from the issue. I get the same error as before. The only difference in the error is the reference to RokuCmd is changed to WebPost. I think this removes RokuCmd from causing the problem.

          Comment


            #6
            You're passing in the URL to POST to from the passed parameters. Make sure it's what you think it should be - print it out using hs.WriteLog("url", url).

            Comment


              #7
              I validated the url and it is as expected. When I copy the url to a browser it works.

              Comment


                #8
                Is it a secure (https) URL, and is your server running an older version of Windows? If so, check out the solutions in this thread:

                https://forums.homeseer.com/forum/we...topped-working

                Comment


                  #9
                  The url is not a secure (https) destination. I am actually sending this request to a local Roku (video streaming) device. I will look at the forum you sent the link for to see if it has any info that may help.

                  Thanks

                  Comment


                    #10
                    Add Try ... Catch exception handling, it might provide a better description of where the error is occurring.
                    Note, the Imports, the "myWriteLog" subroutine and "responsetxt" string weren't defined in the code you posted.


                    Code:
                    Imports System.IO  
                    Imports System.Net  
                    Imports System.Text  
                    
                    Sub WebPost(ByVal url As String)
                    
                        Dim mypost As String = ""
                        Dim postbytes As Byte() = Encoding.UTF8.GetBytes(mypost)
                    
                        Dim tempCookies As New CookieContainer
                    
                        Dim request As HttpWebRequest = HttpWebRequest.Create(url)
                        request.ContentType = "text/html; charset=UTF-8"
                        request.Method = "POST"
                        request.ContentLength = postbytes.Length
                        request.KeepAlive = True
                        request.AllowAutoRedirect = False
                        request.CookieContainer = tempCookies
                        request.Referer = url
                        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
                    
                        Try
                            Dim requeststream As Stream = request.GetRequestStream()
                    
                            requeststream.Write(postbytes, 0, postbytes.Length)
                            requeststream.Close()
                            Dim response As HttpWebResponse = request.GetResponse()
                    
                            Dim myreader As New StreamReader(response.GetResponseStream())
                    
                            Dim responsetxt As String = myreader.ReadToEnd
                            hs.WriteLog("response", responsetxt)
                            myreader.Close()
                            response.Close()
                        Catch ex As Exception
                            'Error trapping
                            hs.WriteLogEx("WebPost", ex.Message, "#ff0000")
                        End Try
                    
                    End Sub

                    Comment


                      #11
                      I took your updated script and made a stand-alone test script in homeseer. I can run this script in visual studio using tenscripting and it works as expected with a valid response. I deploy this script to my homeseer scripts directory and run the same script via a manual event with the same input parameter. The script gets an exception and the message is

                      Unable to connect to the remote server

                      I go back and check again that I can communicate with the server and i get a valid ping and then rerun via visual studio and it works.

                      Still puzzled..... I do appreciate your help in trying to figure this out. Thanks

                      Comment


                        #12
                        The error message about missing entry point is misleading, and is generated by HS many times when there is an error somewhere else.

                        If you are sure that the red statement is the one failing, then I would check into your reference to the Stream object. Perhaps it is referenced by tenScripting project automatically by VS, but not by HS. May need an entry in ScriptingReferences=. Let me know if this makes any sense. I think the Stream object is in the System.IO namespace.
                        tenholde

                        Comment

                        Working...
                        X