TransWikia.com

Failed to using powershell to get cookies

Server Fault Asked by pansal on December 30, 2020

I am writing a powershell script to mimic a login process,and then catch data from web pages. I failed at the very begainning, I can’t get into the login pages at all after I set up cookies.

Here is the script I wrote:

First, get the cookies

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
Get-Content .cookie.txt  | foreach {


    $line=$_ -split '/' | select -First 1
    $tokens=$line.Split("`t").TrimEnd()
    $c=@{
     name=$tokens[0]
     value=$tokens[1]
     domain=$tokens[2]
    }
     $cookie = New-Object System.Net.Cookie
     $cookie.Name=$c.name
     $cookie.Value=$c.Value
     $cookie.Domain=$c.domain
     $session.Cookies.Add($cookie)
    }

Then, use Invoke-RestMethod to catch data from web page

$r=Invoke-RestMethod "http://tz.cscec.com/x5/portal/index.w?timestamp=1486704989262" -WebSession $session -UserAgent 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36' 

At last,out file all catched data

$r | Out-File "F:tz.data$page.html"

Here is the content of cookie.txt:

JSESSIONID  XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.tomcat1   tz.cscec.com    /x5 Wed Feb 07 2018 14:47:48 GMT+0800 (中国标准时间)              
justep-remember true    tz.cscec.com    /   Tue Feb 14 2017 10:22:03 GMT+0800 (中国标准时间)              
justep-username anyone  tz.cscec.com    /   Tue Feb 14 2017 14:47:39 GMT+0800 (中国标准时间)              
request-use-base64  false   tz.cscec.com    /   Wed Feb 07 2018 14:47:48 GMT+0800 (中国标准时间)              
yunsuo_session_verify   SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS    .cscec.com  /   Mon Dec 27 2021 02:58:58 GMT+0800 (中国标准时间)

My output file is the page before login. looks that the cookie part didn’t work.
Can anyone would like to look into my code and give me some suggestion Please?

One Answer

I am coming to answer my question myself... the problem is the cookie file, JSESSIONID will be expired in 24 hours. After refreshing the cookie file, I login to the website.

Answered by pansal on December 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP