How to use Python to download file behind ASP.NET form?

33 Views Asked by At

So, I already tried to do this with regular curl from the Linux shell, but it was not working (spoiler, it outputs the same error here in python).

Basically, I:

  • GET the page
  • Parse it and store __VIEWSTATE __VIEWSTATEGENERATOR and __EVENTVALIDATION variables
  • POST the form with these ASP variables and the values that I need.

My current code:

#!/usr/bin/env python
import httpx
from selectolax.parser import HTMLParser

client = httpx.Client()
url = "https://soportefirmadigital.com/sfdj/dl.aspx"
file = "ClientesLinux_DEB64_Rev25.zip"
desc = "Usuarios Linux (DEB 64bits)"
code = 1234

def main():
    # GET request, parse response
    html = HTMLParser(client.get(url).text)

    # Store ASPX values
    viewstategenerator  = html.css_first("input#__VIEWSTATEGENERATOR").attributes['value']
    eventvalidation     = html.css_first("input#__EVENTVALIDATION").attributes['value']
    viewstate           = html.css_first("input#__VIEWSTATE").attributes['value']

    # Fill form
    formdata = {
            "__EVENTTARGET": "ctl00$certContents$LinkButton3",
            "__EVENTARGUMENT": "",
            "__LASTFOCUS": "",
            "__VIEWSTATE": viewstate,
            "__VIEWSTATEGENERATOR": viewstategenerator,
            "__EVENTVALIDATION": eventvalidation,
            "ctl00$certContents$hiddenISO": file,
            "ctl00$certContents$hidden_ISO_URL": "-",
            "ctl00$certContents$ddlInstaladores": desc,
            "ctl00$certContents$txtDescripcion": "",
            "ctl00$certContents$txtSerialNumber": code,
            "ctl00$certContents$hiddenABID": "",
            "ctl00$certContents$chkConfirmo": "on",
            }

    # Send form
    response = client.post(url, data=formdata)
    print(response.text)

main()

I expect this error (because I cant give out the actual ID):

...
<span id="ctl00_certContents_lblErr" class="fieldErr" style="color:#C00000;">Su tarjeta tiene un número inválido, o bien, no está registrada para soporte.  En caso de duda contacte a su Institución Emisora.</span>
...

However, I get (this implies the serial number wasn't even stored):

...
<span id="ctl00_certContents_lblErr" class="fieldErr"><font color="#C00000">Debe digitar el número de serie de su tarjeta</font></span>
...
0

There are 0 best solutions below