Bor*_*ard 193 windows powershell curl
curl在 PowerShell 中是否有等价物?它有一些类似的内置功能,还是有3次聚会cmdlet的?
小智 130
PowerShell 3.0 有新命令Invoke-RestMethod:
http://technet.microsoft.com/en-us/library/hh849971.aspx
更多详情:
https://discoposse.com/2012/06/30/powershell-invoke-restmethod-putting-the-curl-in-your-shell/
Joh*_*ley 72
从 Powershell 5.0 开始,如果不是之前的话,curl是Invoke-WebRequest.
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
Run Code Online (Sandbox Code Playgroud)
要使用无别名命令...
PS> Invoke-WebRequest -Uri https://localhost:443/
PS> Invoke-WebRequest -Uri https://www.google.com
Run Code Online (Sandbox Code Playgroud)
所以返回请求的几个属性如下...
PS> Invoke-WebRequest -Uri https://www.google.com
StatusCode : 200
StatusDescription : OK
Content : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-AU"><head><meta content="text/html; charset=UTF-8"
http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/...
RawContent : HTTP/1.1 200 OK
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Vary: Accept-Encoding
Run Code Online (Sandbox Code Playgroud)
......或者只是内容......
PS> Invoke-WebRequest -Uri https://www.google.com | Select-Object -ExpandProperty Content
<!doctype html><html itemscope="" itemtype="http://schem[etc ...]
Run Code Online (Sandbox Code Playgroud)
等效的别名命令是...
PS> curl -Uri https://www.google.com
PS> curl -Uri https://www.google.com | Select-Object -ExpandProperty Content
Run Code Online (Sandbox Code Playgroud)
利用 Powershell 默认值和其他别名,您可以将命令缩短为
PS> curl https://www.google.com
ps> curl https://www.google.com | Select -ExpandProperty Content
Run Code Online (Sandbox Code Playgroud)
......但我不会推荐它。详细命令可以帮助其他人阅读您的代码。
更新:
从Powershell 6.x 开始,“Core” curl不再是 的别名Invoke-WebRequest(别名wget也被删除)。而是Invoke-WebRequest直接使用。
PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize
CommandType Name Version Source
----------- ---- ------- ------
Alias iwr -> Invoke-WebRequest
Run Code Online (Sandbox Code Playgroud)
Curl 不再是 Invoke-WebRequest 的别名(在 Powershell 6.2.3 上测试),尽管在RFC 中明显拒绝了“从 Windows PowerShell 中删除别名 curl 和 wget”的动议。
该 RFC 指出“wget/curl 别名已经从 PowerShell Core 中删除,因此[拥有这些别名的] 问题仅限于 Windows PowerShell。”
最后,Powershell 团队还鼓励用户“不要依赖脚本中的别名”。
正如@v6ak 在评论中所指出的curl,wget在 PowerShell(5.0 或更低版本)中使用和可能存在以下问题:如果并排安装,会无意调用真正的 curl 或 wget;并且在任何情况下都会引起混乱。
建议您升级 Powershell“核心”(6.x 或更高版本),以便utf8NoBOM在使用Invoke-WebRequest(以及许多其他文本输出命令)时利用默认编码。如果有人明确这样做,您可以执行以下操作:
Invoke-WebRequest `
-Uri https://raw.githubusercontent.com/fancyapps/fancybox/master/dist/jquery.fancybox.min.js `
| Select-Object -ExpandProperty Content `
| Out-File jquery.fancybox.min.js `
-Encoding utf8NoBOM
Run Code Online (Sandbox Code Playgroud)
但是,即使使用更短的、隐式的命令......
Invoke-WebRequest `
-Uri https://raw.githubusercontent.com/fancyapps/fancybox/master/dist/jquery.fancybox.min.js `
-OutFile jquery.fancybox.min.js
Run Code Online (Sandbox Code Playgroud)
...utf8NoBOM将完成编码(您可以验证这一点,例如,通过在 Visual Studio Code 中打开保存的文件并在状态栏中观察“UTF-8”)。
utf8NoBOM在各种生态系统中旅行时,保存的文件往往会导致较少的问题。当然,如果您需要一些其他编码,您可以显式设置一些替代方案。
在 Powershell 5.0 及更低版本中,utf8NoBOM编码不可用,更不用说默认了。
细节:
ipr*_*101 31
优秀的命令行功夫博客有一篇文章比较了 curl、wget 和相关的 PowerShell 命令
简而言之:
(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html","C:\hello-world.html")
Run Code Online (Sandbox Code Playgroud)
或者,如果您的 Powershell/.Net 版本不接受以下 2 个参数DownloadString:
(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html") > "C:\hello-world.html"
Run Code Online (Sandbox Code Playgroud)
小智 15
您也可以安装Git for Windows,然后将 Git bin 文件夹放在您的路径中。Git 安装包括 curl.exe 等。安装后,只需放入%programfiles(x86)%\git\bin您的PATH。然后,您将能够从 Windows 命令提示符或 PowerShell 控制台使用 curl 命令。
| 归档时间: |
|
| 查看次数: |
451881 次 |
| 最近记录: |