我想尝试将我的 bash 脚本从 linux 转移到 powershell,但不明白为什么它失败了。
Linux命令:
wget -q -x --user-agent="blablabla" --keep-session-cookies --load-cookies cook.txt http://site.com/qqq
电源 shell 代码:
$source = "http://site.com/qqq"
$destination = "d:\site\qqq"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $destination)
但此代码仅下载没有 cookie 的页面。而且我找不到如何将 PHPSESSID 发送到站点。
请向我解释如何做到这一点。
最佳答案
好的,所以你有两个选择。
复制 wget 标志设置的功能。
第一个很简单,只需下载 Windows version of wget .
这是第二个的代码。
$source = "http://site.com/qqq"
$destination = "d:\site\qqq"
$wc = New-Object System.Net.WebClient
# Single Example
$wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "name=value")
# Multi Example
$wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "name=value; name2=value2");
$wc.DownloadFile($source, $destination)
查看 cookies.txt 文件以获取名称值对。
我不知道如何复制
-x
wget
的功能.试试上面的方法,看看它在下载后对文件做了什么。注意 - 我无法测试这个......
关于powershell - 如何在 PowerShell 中使用 cookie 执行 wget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863455/