Welcome Forums General PowerShell Q&A Invoke-WebRequest

This topic contains 3 replies, has 2 voices, and was last updated by

1 year, 9 months ago.

  • Author
  • #70816

    Points: 0
    Rank: Member


    I have a question about Invoke-WebRequest cmdlet. I'm trying to use it to login into a website, and download a report (Excel file).
    To login I'm using POST method, and I don't have a problem with this part, but in the next step when I'm trying to download the file, instead of getting my data, the excel file contains login page...
    Any idea where I'm making a mistake?

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $reportxlsx = "C:\My Files\report.xlsx"
    $r = Invoke-WebRequest -Uri -SessionVariable st
    $form = $r.Forms[0]
    $r = Invoke-WebRequest -Uri ("https://www.mywebsite/forms/login.fcc" + $form.Action) -WebSession $st -Method Post -Body $form.Fields -OutFile $Text
    $r = Invoke-WebRequest -Uri ("") -WebSession $st -Method Get -OutFile $reportxlsx
  • #70850

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Yup. You aren't saving the login cookie and then passing it back on subsequent requests. See the example in the help file for that command for a Facebook example.

  • #70898

    Points: 0
    Rank: Member

    Hi Don,

    I thought that's what the

     -SessionVariable st 

    is doing? I actually did follow all the steps from the help file (Facebook example) and I'm still getting this error:

    Invoke-WebRequest : The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

  • #70901

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Ah, you didn't share that error earlier. The server response appears to be formed in a way that the command doesn't like.
    I'd honestly have to troubleshoot this using a low level tool like Postman, so I could see all the raw headers and whatnot. I'd need to ensure the cookie was going into the request header correctly and see what the server is actually producing. It's not gonna be simple :(.

The topic ‘Invoke-WebRequest’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort