1

I am making requests to a website created using Asp.Net. I am using a Python Requests session to get the __VIEWSTATE and __EVENTVALIDATION variables and add them back to the data payload.

response = s.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'lxml')
viewstate = soup.find(id='__VIEWSTATE').get('value')
eventvalidation = soup.find(id='__EVENTVALIDATION').get('value')
payload.update({'__VIEWSTATE': viewstate, '__EVENTVALIDATION': eventvalidation})
session.post(url, headers=headers, data=payload)

This code works great until I do an action on the form that has an onchange of javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$Chooser$Segment\',\'\')', 0). I have to perform this on a few input's so there are multiple eventTarget.

When I modify any form element that is attached to __doPostBack() function, I receive message of:

Invalid postback or callback argument

from Asp.Net.

How do I simulate multiple __doPostBack's so my __VIEWSTATE and __EVENTVALIDATION do not return an error?

Bijan
  • 7,737
  • 18
  • 89
  • 149
  • Try making a generic get request before the post and set new viewstate and eventvalidation parameters. I had to login on a website using asp and it required me to login with one viewstate and event validation and then make a second get request to get new viewstate and eventval params before my final post. – bbd108 Sep 21 '21 at 01:57
  • Looks like I need to save the `__VIEWSTATE` and `__EVENTVALIDATION` for every `POST` I make and also update `__EVENTTARGET`. It is easy to tell what requests you have to make by looking at the Network Requests in Dev Tools. – Bijan Sep 21 '21 at 20:29
  • yep, makes sense. I imagine other requests may also incur the same functionality, so its just a matter of keeping track of those parameters – bbd108 Sep 22 '21 at 19:53

1 Answers1

1

The ASP net has hidden input, which when you submit the response, you need to take all those inputs back to ASP.

So when you scrap the web, get the response, and process those contents

    def __get_hidden_input(self, content):
        """ Return the dict contain the hidden input 
        """
        tags = dict()
        soup =BeautifulSoup(content, 'html.parser')
        hidden_tags = soup.find_all('input', type='hidden')
        # print(*hidden_tags)
        for tag in hidden_tags:
            tags[tag.get('name')] = tag.get('value')
        
        return tags


Then update these dict data alone with other form data requested. Such as __EVENTTARGET, then send it back to the server.

            r=sess.get(base_frm)
            attrs = self.__get_hidden_input(r.content)
            attrs.update({'__EVNETDATA':'your_target'})
            r=sess.post(base_frm, data=attrs)