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.

1
  • 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. Commented Apr 25, 2014 at 22:05

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And is it possible to make a request to https pages with the above?
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();
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

2 Comments

Short and sweet, and works with a query string appended to the address too.
PowerShell 4 > wget is "sim-linked" to Invoke-WebRequest
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

Comments

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.

Comments

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

Comments

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

Comments

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

Comments

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.