Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Powershell - Post Method - Server not Accepting the Cookie

$
0
0

Hi,

I am trying to download the contents of an ASP page by using HTTP POST method in Powershell.

Initially to get the SessionId, I request the webpage with default GET method. I extract the SessionId (which is part of the cookie) from HTTP response string.

Now I need to submit the form in my script. I send form data as POST method parameters. I am using this received SessionId to request the page with POST method. But I am not getting the desired page (after form submit) with this SessionId. When I use the SessionId present in browser as part of POST header it is working fine.

Can someone please help me to fix this?

Here is my code.

 

  # Get SessionId from HTTP Response Header

$url ='http://example.aspx'
$req =[System.Net.HttpWebRequest][System.Net.WebRequest]::Create($url)
$probe = $html.ReadToEnd()
$cookie = $ResponseObject.Headers["Set-Cookie"]
$strt = $cookie .indexOf(';',0)
$sessionid= $HeaderStr.Substring(0,$strt)#Create HTTP Header with SessionId returned by the server
$req.method ="POST"
$req.Accept='*/*'
$req.Referer='http://example.aspx'
$req.UserAgent='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)  Chrome/36.0.1985.125 Safari/537.36'
$req.ContentType='application/x-www-form-urlencoded'
$req.ContentLength= $buffer.length 
$req.TimeOut=50000
$req.Headers.Add("X-Requested-With: XMLHttpRequest")
$req.Headers.Add("Cache-Control: no-cache")
$req.Headers.Add("X-MicrosoftAjax: Delta=true")
$req.Headers.Add("Origin: http://example.us")
$req.Headers.Add("Accept-Encoding: gzip,deflate,sdch")
$req.Headers.Add("Accept-Language: en-US,en;q=0.8")
$req.Headers.Add("Cookie", $sessionid)#HTTP writing to the stream and capturing response codes lines are here...

The problem is in the last line where I am giving the SessionId in variable. If I give the SessioId stored in browser, it works fine.

Example: In my browser it is: ASP.NET_SessionId=wuimj50yq3aeciveim0pof30 (Works fine if I give it directly)

Script generated is: ASP.NET_SessionId=iqvni2mfozchembnhysv12q2 (Doesn't work)

Thanks for your help


Viewing all articles
Browse latest Browse all 6937

Trending Articles