21

I am looking to make http requests to web pages with powershell, is this possible and if so, how may I achieve this?

Can I make requests to https pages? I am able to make http requests with a bat file but not https, was hoping I could https page requests with powershell.

amateur
  • 43,371
  • 65
  • 192
  • 320
  • Are you requesting content from sites with certificates signed by well-known Certificate Authorities? Browsers and most HTTP stacks balk at retrieving content from HTTPS sites with incorrect, expired, or self-signed (test) certificates. You can usually set a policy to ignore the certificate issue or import the certificate in question, though. – NerdDad Apr 25 '14 at 22:05

8 Answers8

17

You can use the usual WebRequest and HttpWebRequest classes provided by the .NET framework.

$request = [System.Net.WebRequest]::Create('http://example.com')
# do something with $request

It's no different from using the same classes and APIs from C#, except for the syntactic differences to PowerShell.

PowerShell v3 also brings Invoke-WebRequest and a few others.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • And is it possible to make a request to https pages with the above? – amateur Oct 10 '11 at 16:25
  • 1
    yes, see http://stackoverflow.com/questions/560804/how-do-i-use-webrequest-to-access-an-ssl-encrypted-site-using-https – Lars Truijens Oct 10 '11 at 17:53
  • 1
    Just in case anyone needs to send a POST request, since that was the next thing I needed: $request = [System.Net.WebRequest]::Create('mypageurl'); $request.Method = "POST"; $request.ContentType = "application/x-www-form-urlencoded"; $bytes = [System.Text.Encoding]::ASCII.GetBytes("name=john&number=5"); $request.ContentLength = $bytes.Length; $requestStream = $request.GetRequestStream(); $requestStream.Write( $bytes, 0, $bytes.Length ); $requestStream.Close(); $request.GetResponse(); – Clarence Liu Feb 05 '14 at 23:13
16

Try this:

(New-Object System.Net.WebClient).DownloadString("http://stackoverflow.com")

WebClient.DownloadString Method (String)

or in PowerShell 3.0,

(Invoke-WebRequest http://stackoverflow.com).content

Invoke-WebRequest

KIM Taegyoon
  • 1,917
  • 21
  • 18
7

Depending on what you are doing, you can also use System.Net.WebClient, which is a simplified abstraction of HttpWebRequest

$client = new-object system.net.webclient

Look here for difference: What difference is there between WebClient and HTTPWebRequest classes in .NET?

PS: With Powershell v3.0, you have Invoke-WebRequest and Invoke-RestMethod cmdlets which can be used for similar purposes

Community
  • 1
  • 1
manojlds
  • 290,304
  • 63
  • 469
  • 417
2

If all else fails, use Curl from http://curl.haxx.se . You can set everything, including certificate handling, POSTs, etc. Not subtle, but it works and handles all of the odder cases; e.g. you can set the --insecure flag to ignore certificate name issues, expiration, or test status.

NerdDad
  • 464
  • 2
  • 8
2

You can create HTTP, HTTPS, FTP and FILE requests using Invoke-WebRequest cmdlet. This is pretty easy and gives many options to play around. Example: To make simple http/https requests to google.com

Invoke-WebRequest -Uri "http://google.com"

More references can be found MSDN

Gaurav
  • 29
  • 6
0

This code works with both ASCII & binary files over https in powershell:

# Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http

# Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient

# Get the web content.
$task = $client.GetByteArrayAsync("https://stackoverflow.com/questions/7715695/http-requests-with-powershell")

# Wait for the async call to finish
$task.wait();

# Write to file
[io.file]::WriteAllBytes('test.html',$task.result)

Tested on Powershell 5.1.17134.1, Win 10

Zimba
  • 2,854
  • 18
  • 26
0

Try this PowerShell module: https://github.com/toolkitx/simple-request

Install by Install-Module -Name SimpleRequest Then you can send requests like

$Data = @{
    "TokenUrl"     = 111
    "ClientSecret" = "222"
    "ClientId"     = "333"
    "AuthResource" = "444"
    "Username"     = "User1"
    "Password"     = "Password"
    "Id"           = 99
    "Price"        = 0.99
    "Value"        = "Content"
}

$Sample = '
POST https://httpbin.org/post?id={{Id}}

Content-Type: application/json
Authorization: Bearer {{QIBToken}}

{
    "id": {{Id}},
    "value": "{{Value}}"
}'

$Response = Invoke-SimpleRequest -Syntax $Sample -Context $Data

Please refer to GitHub for detail introductions

0

This method downloads the content:

# PowerShell 2 version
$WebRequest=New-Object System.Net.WebClient
$WebRequest.UseDefaultCredentials=$true
#$WebRequest.Credentials=(Get-Credential)
$Data=$WebRequest.DownloadData("http://<url>")
[System.IO.File]::WriteAllBytes("<full path of file>",$Data)

# PowerShell 5 version
Invoke-WebRequest -Uri "http://<url>" -OutFile "<full path of file>" -UseDefaultCredentials -ContentType