How to do wget with cookies in PowerShell

核能气质少年 提交于 2019-12-22 04:56:15

问题


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.


回答1:


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...



来源:https://stackoverflow.com/questions/16863455/how-to-do-wget-with-cookies-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!