当前位置: 代码网 > it编程>前端脚本>Powershell > PowerShell中使用curl(Invoke-WebRequest)的方法教程

PowerShell中使用curl(Invoke-WebRequest)的方法教程

2024年05月15日 Powershell 我要评论
前言powershell能干什么呢?powershell首先是个shell,定义好了一堆命令与操作系统,特别是与文件系统交互,能够启动应用程序,甚至操纵应用程序;第二,powershell允许将几个命

前言

powershell能干什么呢?powershell首先是个shell,定义好了一堆命令与操作系统,特别是与文件系统交互,能够启动应用程序,甚至操纵应用程序;第二,powershell允许将几个命令组合起来放到文件里执行,实现文件级的重用,也就是说有脚本的性质;第三,powershell能够能够充分利用.net类型和com对象,来简单地与各种系统交互,完成各种复杂的、自动化的操作。

当我们习惯了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

-method get

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com