Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do wget with cookies in PowerShell

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.

like image 216
user2441498 Avatar asked May 31 '13 18:05

user2441498


1 Answers

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

like image 70
Andy Arismendi Avatar answered Sep 23 '22 06:09

Andy Arismendi