如何在 PowerShell 中使用 cookie 执行 wget [英] How to do wget with cookies in PowerShell

查看:170
本文介绍了如何在 PowerShell 中使用 cookie 执行 wget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试将我的 bash 脚本从 linux 转移到 powershell,但不明白为什么会失败.

Linux 命令:

wget -q -x --user-agent="blablabla" --keep-session-cookies --load-cookies cook.txt http://site.com/qqq

powershell 代码:

$source = "http://site.com/qqq"$destination = "d:\site\qqq"$wc = 新对象 System.Net.WebClient$wc.DownloadFile($source, $destination)

但此代码只下载没有cookies的页面.而且我找不到如何将 PHPSESSID 发送到站点.

请向我解释如何操作.

解决方案

好的,您有两个选择.

  1. 在 Windows 上运行 wget.
  2. 将 webclient 对象的属性设置为复制 wget 标志设置的功能.

第一个很简单,只需下载 Windows 版本的 wget.>

这是第二个的代码.

$source = "http://site.com/qqq"$destination = "d:\site\qqq"$wc = 新对象 System.Net.WebClient# 单个示例$wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "name=value")# 多个示例$wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "name=value; name2=value2");$wc.DownloadFile($source, $destination)

查看 cookies.txt 文件以获取名称值对.

我不确定如何复制 wget-x 功能.试试上面的方法,看看它在下载后对文件做了什么.

注意 - 我无法测试这个...

I want try transfer my bash script from linux to powershell but cant understand why it failed.

Linux command:

wget  -q -x  --user-agent="blablabla" --keep-session-cookies --load-cookies cook.txt http://site.com/qqq

powershell code:

$source = "http://site.com/qqq"
$destination = "d:\site\qqq" 

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $destination)

but this code only download page without cookies. And i cant find how i can send PHPSESSID to site.

Please explain to me how to do it.

解决方案

Ok so you have two options.

  1. Run wget on Windows.
  2. Set properties on the webclient object to replicate the functionality set by the wget flags.

The first one is easy, just download the Windows version of wget.

Here is the code for the second one.

$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)

Look inside your cookies.txt file to get the name value pairs.

I'm not sure how to replicate the -x functionality of wget. Give the above a try and see what it does with the file after downloading.

Note - I can't test this...

这篇关于如何在 PowerShell 中使用 cookie 执行 wget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆