VB Magic


A VB example to access the azure management REST API

Filed under: .NET, Azure, VB.NET — Tags: , , , — vbmagic @ 11:54 am


This is based on an article in the Code Quick Start from MSDN, Original article is here: Code Quick Start: Create a console application that lists your Windows Azure hosted services

Below is the code which has been converted to VB.net. Everything else in the article is the same apart from creating a VB console program.

Imports System.Net
Imports System.Security.Cryptography.X509Certificates
Imports System.IO

Module Module1

    Sub Main()

            ' X.509 certificate variables
            Dim certStore As X509Store
            Dim certCollection As X509Certificate2Collection
            Dim certificate As X509Certificate2

            ' Request and response variables
            Dim httpRequest As HttpWebRequest
            Dim httpResponse As HttpWebResponse

            ' Stream variables
            Dim responseStream As Stream
            Dim reader As StreamReader

            ' URI variable
            Dim requestURI As Uri

            ' Specify operation to use for the service management call.
            ' This sample will use the operation for listing the hosted services.
            Dim operation As String = "hostedservices"

            ' The ID for the Windows Azure subscription.
            Dim subscriptionId As String = "{Your Subscription ID}"

            ' The thumbprint for the certificate. This certificate would have been
            ' previously added as a management certificate within the Windows
            ' Azure management portal.
            Dim thumbPrint As String = "{Your Certificate Thumbprint}"

            ' Open the certificate store for the current user.
            certStore = New X509Store(StoreName.My, StoreLocation.CurrentUser)

            ' Find the certificate with the specified thumbprint
            certCollection = certStore.Certificates.Find(

            ' close the certificate store

            ' Check to see if mat
            If certCollection.Count = 0 Then
                Throw New Exception("No certificate found containing thumbprint " _
                                    & thumbPrint)
            End If

            ' A matching certificate was found.
            certificate = certCollection(0)
            Console.WriteLine("Using certificate with thumbprint: " & thumbPrint)

            ' create new request
            requestURI = New Uri(
                              subscriptionId, operation))
            httpRequest = HttpWebRequest.Create(requestURI)

            ' add certificate to requrest

            ' Specify the version information in the header
            httpRequest.Headers.Add("x-ms-version", "2011-10-01")

            ' Make the call using the web request
            httpResponse = httpRequest.GetResponse

            ' Display the response status code
            Console.WriteLine("Response status code: " _
                              & httpResponse.StatusCode)

            ' Display thr request ID returned by windows azure
            If httpResponse.Headers IsNot Nothing Then
                Console.WriteLine("x-ms-request-id: " _
                                  & httpResponse.Headers("x-ms-request-id"))
            End If

            ' Parse the web response
            responseStream = httpResponse.GetResponseStream
            reader = New StreamReader(responseStream)
            ' Displa the raw response
            Console.WriteLine("Response output:")

            ' close the resources no longer needed
        Catch ex As Exception
            Console.WriteLine("Error encountered: " & ex.Message)
        End Try
    End Sub

End Module

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: