CorteX Developer Forum Community

Visual Basic Sample Code using CorteX

CorteX Developer

Forums and files for CorteX Developers.

Visual Basic Sample Code using CorteX

  • rated by 0 users
  • This post has 3 Replies |
  • 3 Followers
  • Here is a full Visual Basic Module that includes the 4 Functions that execute the GET, POST, PUT, and DELETE methods for CorteX.  It also contains a Sub Main() entry point to run the code.  This example can be copied into a Visual Studio project and run after the password and IP Address values are updated to point to the device properly.

    Enjoy!

    Jeff

    ======================= Code Starts Here ================================

    Imports System.IO

    Imports System.Xml

     

     

    Module Module1

     

        'Start Function ExecuteCortexGet ******************************************************************************************

        Public Function ExecuteCortexGet(ByVal fullUrl As String)

            Dim webRequest As System.Net.HttpWebRequest

            Dim webResponse As System.Net.HttpWebResponse

            Dim creds As New System.Net.NetworkCredential("administrator", "password")

     

            Try

                'Create an HttpWebRequest with the specified URL.

     

                webRequest = CType(System.Net.WebRequest.Create(fullUrl), System.Net.HttpWebRequest)

                'Add Credentials to the webRequest

                webRequest.Credentials = creds

                'Set the Method (Default is GET, but showing explicitly here as an example for the other methods

                webRequest.Method = "GET"

     

                'Send the request and wait for a response.

                Try

                    webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)

                    Select Case (webResponse.StatusCode)

                        Case System.Net.HttpStatusCode.OK

                            'read the content from the response

                            Dim responseStream As System.IO.Stream = webResponse.GetResponseStream()

                            Dim responseEncoding As System.Text.Encoding = System.Text.Encoding.UTF8

                            ' Pipes the response stream to a higher level stream reader with the required encoding format.

                            Dim responseReader As New StreamReader(responseStream, responseEncoding)

                            Dim responseContent As String = responseReader.ReadToEnd()

                            Return responseContent

                        Case System.Net.HttpStatusCode.NotFound

                            Throw New System.Exception(String.Format("Unable to read response content. URL not found. StatusCode={0}.", webResponse.StatusCode))

                        Case Else

                            Throw New System.Exception(String.Format("Unable to read response content. StatusCode={0}.", webResponse.StatusCode))

                    End Select

                Catch we As System.Net.WebException

                    'If (we.Status = Net.WebExceptionStatus.Timeout) Then

                    '    Return False

                    'End If

                    Throw New System.Exception("Unable to execute URL.", we)

                Finally

                    If (Not IsNothing(webResponse)) Then

                        webResponse.Close()

                    End If

                End Try

            Catch e As System.Exception

                Throw New System.Exception("Unable to execute URL.", e)

     

            End Try

        End Function

        'End Function ExecuteCortexGet ********************************************************************************************

     

        'Start Function ExecuteCortexPut ******************************************************************************************

        Public Function ExecuteCortexPut(ByVal fullUrl As String)

            Dim webRequest As System.Net.HttpWebRequest

            Dim webResponse As System.Net.HttpWebResponse

            Dim creds As New System.Net.NetworkCredential("administrator", "password")

     

            Try

                'Create an HttpWebRequest with the specified URL.

     

                webRequest = CType(System.Net.WebRequest.Create(fullUrl), System.Net.HttpWebRequest)

                'Add Credentials to the webRequest

                webRequest.Credentials = creds

                'Set the Method (Default is GET, but showing explicitly here as an example for the other methods

                webRequest.Method = "PUT"

     

                'Send the request and wait for a response.

                Try

                    webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)

                    Select Case (webResponse.StatusCode)

                        Case System.Net.HttpStatusCode.Created

                            'read the content from the response

                            Dim responseStream As System.IO.Stream = webResponse.GetResponseStream()

                            Dim responseEncoding As System.Text.Encoding = System.Text.Encoding.UTF8

                            ' Pipes the response stream to a higher level stream reader with the required encoding format.

                            Dim responseReader As New StreamReader(responseStream, responseEncoding)

                            Dim responseContent As String = responseReader.ReadToEnd()

                            Return responseContent

                        Case System.Net.HttpStatusCode.NotFound

                            Throw New System.Exception(String.Format("Unable to read response content. URL not found. StatusCode={0}.", webResponse.StatusCode))

                        Case Else

                            Throw New System.Exception(String.Format("Unable to read response content. StatusCode={0}.", webResponse.StatusCode))

                    End Select

                Catch we As System.Net.WebException

                    'If (we.Status = Net.WebExceptionStatus.Timeout) Then

                    '    Return False

                    'End If

                    Throw New System.Exception("Unable to execute URL.", we)

                Finally

                    If (Not IsNothing(webResponse)) Then

                        webResponse.Close()

                    End If

                End Try

            Catch e As System.Exception

                Throw New System.Exception("Unable to execute URL.", e)

            End Try

        End Function

        'End Function ExecuteCortexPut ******************************************************************************************

     

     

        'Start Function ExecuteCortexPost ******************************************************************************************

        Public Function ExecuteCortexPost(ByVal fullUrl As String, ByVal queryString As String)

            Dim webRequest As System.Net.HttpWebRequest

            Dim webResponse As System.Net.HttpWebResponse

            Dim creds As New System.Net.NetworkCredential("administrator", "password")

     

            Try

                'Create an HttpWebRequest with the specified URL.

     

                webRequest = CType(System.Net.WebRequest.Create(fullUrl), System.Net.HttpWebRequest)

                'Add Credentials to the webRequest

                webRequest.Credentials = creds

                'Set the Method (Default is GET, but showing explicitly here as an example for the other methods

                webRequest.Method = "POST"

                webRequest.ContentType = "text/html"

     

                Dim PostStr = System.Text.Encoding.UTF8.GetBytes(queryString)

                webRequest.ContentLength = PostStr.Length

     

                Dim requestStream = webRequest.GetRequestStream()

                requestStream.Write(PostStr, 0, PostStr.Length)

                requestStream.Close()

     

                'Send the request and wait for a response.

                Try

                    webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)

                    Select Case (webResponse.StatusCode)

                        Case System.Net.HttpStatusCode.OK Or System.Net.HttpStatusCode.Created

                            'Return the newly created volume URI for further use by the main program

                            Return webResponse.Headers("Location")

                        Case System.Net.HttpStatusCode.NotFound

                            Throw New System.Exception(String.Format("Unable to read response content. URL not found. StatusCode={0}.", webResponse.StatusCode))

                        Case Else

                            Throw New System.Exception(String.Format("Unable to read response content. StatusCode={0}.", webResponse.StatusCode))

                    End Select

                Catch we As System.Net.WebException

                    Throw New System.Exception("Unable to execute URL.", we)

                Finally

                    If (Not IsNothing(webResponse)) Then

                        webResponse.Close()

                    End If

                End Try

            Catch e As System.Exception

                Throw New System.Exception("Unable to execute URL.", e)

            End Try

        End Function

        'End Function ExecuteCortexPost ******************************************************************************************

     

        'Start Function ExecuteCortexDelete ******************************************************************************************

        Public Function ExecuteCortexDelete(ByVal fullUrl As String)

            Dim webRequest As System.Net.HttpWebRequest

            Dim webResponse As System.Net.HttpWebResponse

            Dim creds As New System.Net.NetworkCredential("administrator", "password")

     

            Try

                'Create an HttpWebRequest with the specified URL.

     

                webRequest = CType(System.Net.WebRequest.Create(fullUrl), System.Net.HttpWebRequest)

                'Add Credentials to the webRequest

                webRequest.Credentials = creds

                'Set the Method (Default is GET, but showing explicitly here as an example for the other methods

                webRequest.Method = "DELETE"

     

                'Send the request and wait for a response.

                Try

                    webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)

                    Select Case (webResponse.StatusCode)

                        Case System.Net.HttpStatusCode.NoContent

                            'read the content from the response

                            Dim responseStream As System.IO.Stream = webResponse.GetResponseStream()

                            Dim responseEncoding As System.Text.Encoding = System.Text.Encoding.UTF8

                            ' Pipes the response stream to a higher level stream reader with the required encoding format.

                            Dim responseReader As New StreamReader(responseStream, responseEncoding)

                            Dim responseContent As String = responseReader.ReadToEnd()

                            Return responseContent

                        Case System.Net.HttpStatusCode.NotFound

                            Throw New System.Exception(String.Format("Unable to read response content. URL not found. StatusCode={0}.", webResponse.StatusCode))

                        Case Else

                            Throw New System.Exception(String.Format("Unable to read response content. StatusCode={0}.", webResponse.StatusCode))

                    End Select

                Catch we As System.Net.WebException

                    'If (we.Status = Net.WebExceptionStatus.Timeout) Then

                    '    Return False

                    'End If

                    Throw New System.Exception("Unable to execute URL.", we)

                Finally

                    If (Not IsNothing(webResponse)) Then

                        webResponse.Close()

                    End If

                End Try

            Catch e As System.Exception

                Throw New System.Exception("Unable to execute URL.", e)

            End Try

        End Function

        'End Function ExecuteCortexDelete ******************************************************************************************

     

        'Start Sub Main ******************************************************************************************

        Sub Main()

     

     

            Dim responseContent As String

            Dim queryString As String

            Dim commentField As String

     

            'Change the URI Ip Address to match an ISE that will participate

            Dim iseIpAddress = "10.10.10.10" 'ISE IP Address here

            Dim iseVolURI As String = "http://" + iseIpAddress + "/storage/volumes"

     

     

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Set up do a GET (retrieve) a the volume list

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            responseContent = ExecuteCortexGet(iseVolURI)

     

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Set up do a POST (create) a new volume

            'The HTTP POST requires the query string (parameters) to be in the Request Body, so the string goes in separately with the URI

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            queryString = "size=33&name=VBCortexTest01"

            'The newVolUri is the returned instance address for the new volume for use with the next functions

            Dim newVolUri As String = ExecuteCortexPost(iseVolURI, queryString)

     

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Set up the URI to do a PUT (modify) of the existing, new volume

            'The HTTP PUT requires the query string (parameters) to be inline with the URI, so the string is appended to the end of the URI with a "?" between the two

            ' ******* THIS EXAMPLE SHOWS HOW TO MODIFY THE COMMENT FIELD OF AN ISE VOLUME ******

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            commentField = "TESTING123"

            queryString = commentField

            responseContent = ExecuteCortexPut(newVolUri + "?" + queryString)

     

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Delete the new volume

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            responseContent = ExecuteCortexDelete(newVolUri)

     

            'DONE with showcasing the 4 main HTTP Methods for operating on an ISE Volume

     

        End Sub

        'End Sub Main ******************************************************************************************

     

    End Module

     

    ======================= Code Ends Here ================================

     

     

     

     

     

  • Is it possible to leave the request open and stream data from the controllers using the GET code?  I am building a monitoring application and every time I refresh the data I get a ~1 second or so pause while the http request is opened, data retrieved, and then closed.

  • Hi,

      So it turns out that .NET will reuse the HttpWebRequest object, if you have the KeepAlive flag set to True.  In the sample below (Sorry, C# :) you'll see that the HttpWebRequest is declared once, then instantiated every time the for loop starts.
      When running this sample code, I don't see any lag (ie, re-establishing / re-connecting the Request object). 
     
    HTH,
    Butch>

            static void Main(string[] args) {
                HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create("
    http://10.20.54.86/query");
                web.KeepAlive = true;
                HttpWebResponse response = null;

                for( int x = 0; x < 20; x++){
                    web = (HttpWebRequest)HttpWebRequest.Create("
    http://10.20.54.86/query");
                    response = (HttpWebResponse)web.GetResponse();
                    using( StreamReader rdr = new StreamReader(response.GetResponseStream())){
                        string body = rdr.ReadToEnd();
                        Console.WriteLine(DateTime.Now.ToString() + ", " + body);
                        rdr.Close();
                    }//end using (StreamReader)
                    response.Close();
                    System.Threading.Thread.Sleep(500); // sleep 1/2 second
                }//end for(20x)
            }//end Main()

  • I'm still getting the pause after using your method for getting the data from the device.  I think its stemming from the System.Net.NetworkCredential that has to be added for my device.  Also, I think the way that you are running your loop is a different approach to what I am doing.  I'm using a thread external to the ExecuteCortexGet which is closing the session after the function exits each time it is looped.

    I have got a base going now for what I want to accomplish though, thanks for the help.  I'm glad to know that we can get responses on this forum!

Page 1 of 1 (4 items)