Skip to content

Instantly share code, notes, and snippets.

@lennartvdd
Created April 15, 2014 09:18
Show Gist options
  • Save lennartvdd/10716777 to your computer and use it in GitHub Desktop.
Save lennartvdd/10716777 to your computer and use it in GitHub Desktop.
Powershell Web Request
#param(
# [string]$ProductId,
#)
# ---- Basic Settings
$url = ""
$username = ""
$password = ""
$timeout = 60 * 1000 # 60 seconds
# ---- ./ Basic Settings
# ---- Build URL Params
$params = ""
#if ($ProductId) {
# $params = "?product_id=" + $ProductId
#}
# ---- ./ Build URL Params
# ---- HTTP Request Logic ---- DO NOT CHANGE BELOW
$url = $url + $params
$(get-date -UFormat "%d-%m-%Y %R") + " [INFO] Request to: $url"
$credentials = $FALSE
if($username -or $password)
{
$(get-date -UFormat "%d-%m-%Y %R") + " [INFO] Using Authentication"
$credentials = New-Object -TypeName System.Net.NetworkCredential
$credentials.UserName = $username
$credentials.Password = $password
}
$request = [System.Net.WebRequest]::Create($url)
if($credentials) {
$request.Credentials = $credentials;
}
$request.Timeout = $timeout
# Get Response
$response = $request.GetResponse()
$responseCode = ($response.Statuscode) -as [int]
$responseStream = $response.GetResponseStream()
# Read Response Stream
$streamReader = New-Object System.IO.StreamReader $responseStream
$responseText = $streamReader.ReadToEnd()
# Close streams.
$streamReader.Close()
$responseStream.Close()
$response.Close()
#Show results
"Response (code: " + $responseCode + "): "
"$responseText"
""
$(get-date -UFormat "%d-%m-%Y %R") + " [INFO] Finished"
# ./ ---- HTTP Request Logic ----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment