目录
前言 Invoke-WebRequest简单用法 1.用途 2.语法Syntax 3.简单的几个用法
前言
习惯了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:\Users\rmiao> 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
3.4将获取到的content输出到文件
-OutFile 'c:\Users\rmiao\temp\content.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:\Users\rmiao> $R = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in +a+mile
PS C:\Users\rmiao> $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:\Users\rmiao> $R=curl http://www.facebook.com/login.php -SessionVariable fb
PS C:\Users\rmiao> $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:\Users\rmiao> $Form=$R.Forms[0]
PS C:\Users\rmiao> $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:\Users\rmiao> $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" ] = "User01@Fabrikam.com"
$ 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:\Users\rmiao> $R.StatusDescription
OK
虽然没有curl那么主流,但一样可以成为http访问的一个选择。
参考
https://technet.microsoft.com/en-us/library/hh849901.aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2013-09-17 gcc编译器命令使用详解
2013-09-17 linux gcc 编译时头文件和库文件搜索路径