ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


Powershell HTTP Get and HTTP Post Sample Source Code

ActiveSocket provides an easy-to-use development interface to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.

ActiveSocket features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

HTTP Get and HTTP Post can be well integrated into Powershell environments. This document describes how the ActiveSocket Http object can be integrated into Powershell projects.

The most important functions of the Http object are:

  • Connect - connect to the (remote) HTTP web server on port 80 or any alternate port; optionally, use proxy credentials to use a proxy server; optionally, specify a web account and password for password protected web sites
  • Disconnect - to diconnect after a connect call;
  • ReadData - read all data from a web page;
  • WriteData - write data to a web page.

Step 1: Download and install the ActiveSocket Toolkit

Download ActiveSocket from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new script

Create a new script using your favorite editor. You can simply use notepad. However, a Powershell editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced script to communicate using the ActiveSocket Toolkit.

Step 3: Create the ActiveSocket object in Powershell

Create a new Powershell file called DEMO.PS1.

Create the ActiveSocket object(s) like this:

$objHttp = new-object -comobject ActiveXperts.Http

Now, add the following lines to the file to have your fist ActiveSocket Powershell program:

Write-Host "ActiveSocket Version " $objHttp.Version "; Build " $objHttp.Build "; Module " $objHttp.Module
Write-Host "Expiration date: " $objHttp.ExpirationDate

Step 4: Read data from a web site

The following Powershell code shows how to get an HTML page from a webserver:

#################################################################################
# ActiveSocket - Powershell script
# © Copyright ActiveXperts Software B.V.
#
# For more information about ActiveSocket, please
# visit the online ActiveSocket page at:
# http://www.activexperts.com
#################################################################################
#  HTTP Sample: simple sample to show how to retrieve data from a web 
#  server. Proxy and security are not demonstrated to keep sample small.
#################################################################################

cls

#################################################################################
# Functions --------------------------------------------------------------------#
#################################################################################


#################################################################################
# ReadInput --------------------------------------------------------------------#

function ReadInput($strTitle, $strDefault, $bAllowEmpty)
{
	$strReturn = ""
	do
		{
		     $strInput = Read-host $strTitle, $strDefault
		     if($strInput -ne "") 
				{
		          $strReturn = $strInput
				}
			
			if($bAllowEmpty -eq 1)
				{
					break
				}
		}		
	while($strReturn -eq "")

	return $strReturn
}

#################################################################################
# THE SCRIPT ITSELF ------------------------------------------------------------#
#################################################################################

# Create a HTTP instance
$objHttp = new-object -comobject ActiveXperts.Http

# Write some information to console
Write-Host "ActiveSocket Version " $objHttp.Version "; Build " $objHttp.Build "; Module " $objHttp.Module
Write-Host "Expiration date: " $objHttp.ExpirationDate

$strUrl = ReadInput "Enter a URL" "www.activexperts.com/products"

$objHttp.LogFile = "C:\HttpLog.txt"

# OPTIONAL: settings for proxy server
#
#$objHttp.ProxyServer	= "dell04:8080"
#$objHttp.ProxyAccount	= "Administrator"
#$objHttp.ProxyPassword	= "Secret"

$objHttp.Connect( $strUrl )
Write-Host "Connect, result: " $objHttp.LastError " (" $objHttp.GetErrorDescription($objHttp.LastError) ")"
if( $objHttp.LastError -ne 0 )
	{
		exit
	}

$strData = $objHttp.Readheader( 37 )   
# see also: www.activexperts.com/network-component/tutorials/httpheaders/

if( $objHttp.LastError -ne 0 ) 
	{
		Write-Host "Header(Server): " $strData
	}	

$strData = $objHttp.Readheader( 1 )    
# see also: www.activexperts.com/network-component/tutorials/httpheaders/
if( $objHttp.LastError -eq 0 ) 
	{
		Write-Host "Header(Content-Type): " $strData
	}	

$strData = $objHttp.ReadData()
if( $objHttp.LastError -eq 0 )
	{
		Write-Host $strData
	}	

$objHttp.Disconnect()
Write-Host "Disconnect."

Write-Host "Ready."

To run the code, start Powershell and browse to the location of the file you just created. Enter .\Demo.ps1 to run the code. Notice that if the script is not working, you have to change the execution policy; you can do that with the following command:

Set-ExecutionPolicy -unrestricted

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/network-component.