在PowerShell中使用curl(Invoke-WebRequest)
阿新 • • 發佈:2022-05-04
前言
習慣了windows的介面模式就很難轉去命令列,甚至以命令列發家的git也湧現出各種介面tool。然而命令列真的會比介面快的多,如果你是一個碼農。
situation:接到需求分析bug,需要訪問http。那臺機器屬於product,不允許裝postman。我只能手動命令列來發請求。發現了內建的PowerShell中有curl命令。歡喜試了半天,總是命令不對,google發現這個curl是冒名頂替的,只是一個Invoke-WebRequest的alias。參考。
PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize CommandType Name Version Source ----------- ---- ------- ------ Alias curl -> Invoke-WebRequest Alias iwr -> Invoke-WebRequest Alias wget -> Invoke-WebRequest
Invoke-WebRequest簡單用法
1.用途
Gets content from a web page on the Internet.
獲取http web請求訪問內容
2.語法Syntax
Parameter Set: Default Invoke-WebRequest [-Uri] <Uri> [-Body <Object> ] [-Certificate <X509Certificate> ] [-CertificateThumbprint <String> ] [-ContentType <String> ] [-Credential <PSCredential> ] [-DisableKeepAlive] [-Headers <IDictionary> ] [-InFile <String> ] [-MaximumRedirection <Int32> ] [-Method <WebRequestMethod> {Default | Get | Head | Post | Put | Delete | Trace | Options | Merge | Patch} ] [-OutFile <String> ] [-PassThru] [-Proxy <Uri> ] [-ProxyCredential <PSCredential> ] [-ProxyUseDefaultCredentials] [-SessionVariable <String> ] [-TimeoutSec <Int32> ] [-TransferEncoding <String> {chunked | compress | deflate | gzip | identity} ] [-UseBasicParsing] [-UseDefaultCredentials] [-UserAgent <String> ] [-WebSession <WebRequestSession> ] [ <CommonParameters>]
3.簡單的幾個用法
3.1 Get請求
PS C:Usersrmiao> curl -URi https://www.google.com StatusCode : 200 StatusDescription : OK Content : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many speci..." RawContent : HTTP/1.1 200 OK X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32" Vary: Accept-Encoding Transfer-Encoding: chunked
會發現content內容被截斷了。想要獲取完整的content:
ps> curl https://www.google.com | Select -ExpandProperty Content
3.2新增header
-Headers @{"accept"="application/json"}
3.3指定Method
-Method Get
3.4將獲取到的content輸出到檔案
-OutFile 'c:Usersrmiaotempcontent.txt'
3.5表單提交
For example:
$R = Invoke-WebRequest http://website.com/login.aspx
$R.Forms[0].Name = "MyName"
$R.Forms[0].Password = "MyPassword"
Invoke-RestMethod http://website.com/service.aspx -Body $R
or
Invoke-RestMethod http://website.com/service.aspx -Body $R.Forms[0]
3.6內容篩選
PS C:Usersrmiao> $R = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile
PS C:Usersrmiao> $R.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText -
First 5
innerText
---------
=
1
Next
=
3.7一個登陸示例
#傳送一個登陸請求,宣告一個sessionVariable 引數為fb, 將結果儲存在$R
#這個變數FB就是header.cookie等集合
PS C:Usersrmiao> $R=curl http://www.facebook.com/login.php -SessionVariable fb
PS C:Usersrmiao> $FB
Headers : {}
Cookies : System.Net.CookieContainer
UseDefaultCredentials : False
Credentials :
Certificates :
UserAgent : Mozilla/5.0 (Windows NT; Windows NT 6.3; en-US) WindowsPowerShell/4.0
Proxy :
MaximumRedirection : -1
#將response響應結果中的第一個form屬性賦值給變數Form
PS C:Usersrmiao> $Form=$R.Forms[0]
PS C:Usersrmiao> $Form.fields
Key Value
--- -----
lsd AVqQqrLW
display
enable_profile_selector
isprivate
legacy_return 0
profile_selector_ids
return_session
skip_api_login
signed_next
trynum 1
u_0_0
u_0_1
lgnrnd 214945_qGeg
lgnjs n
email
pass
persistent
default_persistent 1
# 檢視form
PS C:Usersrmiao> $Form | Format-List
Id : login_form
Method : post
Action : /login.php?login_attempt=1&lwv=100
Fields : {[lsd, AVqQqrLW], [display, ], [enable_profile_selector, ], [isprivate, ]...}
#檢視屬性
$Form.fields
#設定賬號密碼
$Form.Fields["email"] = "[email protected]"
$Form.Fields["pass"] = "P@ssw0rd"
#傳送請求並儲存結果為$R
$R=Invoke-WebRequest -Uri ("https://www.facebook.com" + $Form.Action) -WebSession $FB -Method POST -Body $Form.Fields
#檢視結果
PS C:Usersrmiao> $R.StatusDescription
OK
雖然沒有curl那麼主流,但一樣可以成為http訪問的一個選擇。