powershell - 如何在 PowerShell 中使用 cookie 执行 wget

标签 powershell wget

我想尝试将我的 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 发送到站点。

请向我解释如何做到这一点。

最佳答案

好的,所以你有两个选择。

  • 在 Windows 上运行 wget。
  • 将 webclient 对象的属性设置为
    复制 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/

    相关文章:

    linux - 如何在重定向后通过 wget 下载文件名

    linux - 从 Bash 获取文件的直接链接?

    php - 使用php将文件从其他站点下载到Web服务器

    Powershell - 获取广告组成员 - 无法联系服务器

    azure - 如何使突触管理的专用端点 privateLinkResourceId 成为workspace_publish分支中的参数

    powershell - 如何使用 powershell 播放 mp3(简单)?

    backup - 您如何使用wget(带有mk选项)来镜像站点及其外部链接的图像?

    macos - Homebrew 不会运行 wget 命令(库未加载)

    powershell - 在 Powershell 中,按记录类型拆分大型文本文件的最有效方法是什么?

    unit-testing - 如何在powershell中进行TDD和单元测试?