Exploring web services

A web service is generally an XML-based program. Using this, we can exchange data over a network. A web service is nothing but an application component, which works on an open protocol, can be used by other applications, and is reusable.

Web services allow interaction between applications and use standardized XML. Simply put, it's a combination of XML and HTTP. The following components work for all web services:

  • Simple Object Access Protocol (SOAP)
  • Universal Description, Discovery, and Integration (UDDI)
  • Web Services Description Language (WSDL)

In this topic, we will demonstrate the use of web services and complex web services using Windows PowerShell.

Using web services

New-WebServiceProxy is a cmdlet that allows us to create a proxy object of any given valid web service.

For our example, we will use the following web service:

http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

Run the following command:

$var = New-WebServiceProxy -Uri http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
$var.ConversionRate

This returns the overload definitions of the conversion rate method, as shown in the following image:

Using web services

Points marked in the image we just saw are explained as follows:

  • 1: The first argument should be FromCurrency
  • 2: The second argument should be ToCurrency

Here is a simple example of this:

$var = New-WebServiceProxy -Uri http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
$var.ConversionRate('Eur','US')

This returns the output as 1.0979—the current conversion value.

How do we know the currency codes? Just open the following URL in a browser:

http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

This shows the output as illustrated in the following image:

Using web services

Similarly, we can use the GlobalWeather web service to retrieve information:

http://www.webservicex.net/globalweather.asmx?wsdl

Run the following command:

$weather = New-WebServiceProxy -uri http://www.webservicex.com/globalweather.asmx?WSDL
([XML]$weather.GetWeather('Amsterdam','Netherlands')).CurrentWeather

The output is illustrated in the following image:

Using web services

Web services are usually limited to XML and SOAP, but they may be in the JSON format as well. So, it's worth considering an example that returns results in the JSON format.

Let's use an Apple iTunes URL, which returns an output in the JSON format:

http://itunes.apple.com/search?term=metallica

If we open this URL in the browser, we will see the output as shown in the following image:

Using web services

Here, we will not use the Invoke-RestMethod cmdlet because it formats the data by default. Instead, we will use the Invoke-WebRequest cmdlet for the ConvertFrom-Json cmdlet:

$Json = Invoke-WebRequest -Uri "http://itunes.apple.com/search?term=metallica"
$Json

Take a look at the following image:

Using web services

Now, we will play with the PowerShell code to convert this content into a readable format:

$Json = Invoke-WebRequest -Uri "http://itunes.apple.com/search?term=metallica"
($Json | ConvertFrom-Json).Results | Select WrapperType, trackName | Select -First 25

Now, consider the following image:

Using web services

Alternatively, we can use the Invoke-RestMethod cmdlet and do the same:

$Json = Invoke-RestMethod -Uri "http://itunes.apple.com/search?term=metallica"
$Json.results | Select WrapperType , trackName | Select -First 25

Building web services

In this exercise, we will take a look at the steps required to build web services. For this, we will use the following environments:

  • Windows Server 2012
  • Visual Studio 2013
  • The installed IIS server

We configured IIS in WMF5Node03 to demonstrate PowerShell web access; now, let's connect to the same box and build a small web service. Perform the following steps:

  1. Open Visual Studio.
  2. Select .NET framework 3.5.
  3. Then, select ASP.NET Web Service.
  4. Select HTTP and name your site.
  5. In our scenario, we will name it http://localhost/website.

Visual Studio builds the basic web service. You can run the following command:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
     Inherits System.Web.Services.WebService
    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "This is a Basic and Default Web Service!"
    End Function
End Class

Take a look at the following image:

Building web services

So, now we have the URL; we simply changed the Hello World text to something else, as follows:

http://localhost/website/Service.asmx

Okay, spin up PowerShell now!

Building web services

Points marked in the figure are explained as follows:

  • 1: At the end of the URL, append ?wsdl
  • 2: Assign it to the variable named $local
  • 3: Invoke the function HelloWorld()
  • 4: It returns the default output

Invoke after doing text changes in the code which return your newly updated text

Now, in Service.vb, add the following code:

    <WebMethod()> _
    Public Function Add(a, b) As Int32
        Return a + b
    End Function

We can add two values in Windows PowerShell:

$local.Add(2,3)
Building web services

Create your own web service and consume Windows PowerShell to use it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset