Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http requests with powershell

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.

like image 435
amateur Avatar asked Oct 10 '11 16:10

amateur


People also ask

How do I run WebRequest in PowerShell?

To get a certificate thumbprint, use the Get-Item or Get-ChildItem command in the PowerShell Cert: drive. Specifies the content type of the web request. If this parameter is omitted and the request method is POST, Invoke-WebRequest sets the content type to application/x-www-form-urlencoded .

What is IEX in PowerShell?

iex is an alias for Invoke-Expression . Here the two backticks don't make any difference, but just obfuscates the command a little. iex executes a string as an expression, even from pipe. Here Start-Process is a cmdlet that starts processes.

What is $_ PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What is the difference between invoke RestMethod and invoke-WebRequest?

Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.


1 Answers

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.

like image 90
Joey Avatar answered Oct 16 '22 16:10

Joey