How to make wget with cookies in PowerShell

I want to try porting my bash script from linux to powershell, but can't figure out 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 loads pages without cookies. And I can’t find how I can send PHPSESSID to the site.

Please explain to me how to do this.

+7
source share
1 answer

So, you have two options.

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

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

Here is the code for the second.

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

Check your cookie.txt for a pair of name values.

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

Note. I can not check it ...

+11
source

All Articles