Failed to using powershell to get cookies

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
    [email protected]{
     $cookie = New-Object System.Net.Cookie

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

$r=Invoke-RestMethod "" -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 "$page.html"

Here is the content of cookie.txt:

JSESSIONID  XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.tomcat1    /x5 Wed Feb 07 2018 14:47:48 GMT+0800 (中国标准时间)              
justep-remember true    /   Tue Feb 14 2017 10:22:03 GMT+0800 (中国标准时间)              
justep-username anyone    /   Tue Feb 14 2017 14:47:39 GMT+0800 (中国标准时间)              
request-use-base64  false    /   Wed Feb 07 2018 14:47:48 GMT+0800 (中国标准时间)              
yunsuo_session_verify   SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS  /   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?

Server Fault Asked by pansal on December 30, 2020

1 Answers

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!

Related Questions

Forge UDP checksum

1  Asked on November 4, 2021 by wullxz


FFMPEG Install on EC2 – Amazon Linux

4  Asked on November 4, 2021 by oliver-holmberg


How to install Asterisk with SRTP support?

3  Asked on November 4, 2021 by justlogin


CentOS 6.0 only shows loopback address with ifconfig

2  Asked on November 4, 2021 by nakiya


Working with array of object in Zabbix

1  Asked on November 4, 2021 by yury


Ask a Question

Get help from others!

© 2022 All rights reserved.