Can't login to a specific ASP.NET website using python requests
Question: So I've been trying for the last 6 hours to make this work, but I couldn't and
endless searches didn't help, So I guess I'm either doing something very
fundamental wrong, or it's just a trivial bug which happens to match my logic
so I need extra eyes to help me fix it.  
The website url is [this](https://www.wes.org/appstatus/).  
I wrote a piece of messy python code to just login and read the next page, but
All I get is a nasty 500 error saying something on the server went wrong
processing my request.  
Here is the request made by a browser which works just fine, no problem.  
HTTP Response code to this request is 302 (Redirect)

    
    
    POST /appstatus/index.aspx HTTP/1.1
    Host: www.wes.org
    Connection: close
    Content-Length: 303
    Cache-Control: max-age=0
    Origin: https://www.wes.org
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
    Content-Type: application/x-www-form-urlencoded
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Referer: https://www.wes.org/appstatus/index.aspx
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.8,fa;q=0.6
    Cookie: ASP.NET_SessionId=bu2gemmlh3hvp4f5lqqngrbp; _ga=GA1.2.1842963052.1473348318; _gat=1
    
    __VIEWSTATE=%2FwEPDwUKLTg3MTMwMDc1NA9kFgICAQ9kFgICAQ8PFgIeBFRleHRkZGRk9rP20Uj9SdsjOKNUBlbw55Q01zI%3D&__VIEWSTATEGENERATOR=189D346C&__EVENTVALIDATION=%2FwEWBQK6lf6LBAKf%2B9bUAgK9%2B7qcDgK8w4S2BALowqJjoU1f0Cg%2FEAGU6r2IjpIPG8BO%2BiE%3D&txtUID=Email%40Removed.com&txtPWD=PASSWORDREMOVED&Submit=Log+In&Hidden1=
    

and this one is the request made by my script.

    
    
    POST /appstatus/index.aspx HTTP/1.1
    Host: www.wes.org
    Connection: close
    Accept-Encoding: gzip, deflate, br
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Upgrade-Insecure-Requests: 1
    Content-Type: application/x-www-form-urlencoded
    Origin: https://www.wes.org
    Accept-Language: en-US,en;q=0.8,fa;q=0.6
    Cache-Control: max-age=0
    Referer: https://www.wes.org/appstatus/indexca.aspx
    Cookie: ASP.NET_SessionId=nxotmb55jjwf5x4511rwiy45
    Content-Length: 303
    
    txtPWD=PASSWORDREMOVED&Submit=Log+In&__EVENTVALIDATION=%2FwEWBQK6lf6LBAKf%2B9bUAgK9%2B7qcDgK8w4S2BALowqJjoU1f0Cg%2FEAGU6r2IjpIPG8BO%2BiE%3D&txtUID=Email%40Removed.com&__VIEWSTATE=%2FwEPDwUKLTg3MTMwMDc1NA9kFgICAQ9kFgICAQ8PFgIeBFRleHRkZGRk9rP20Uj9SdsjOKNUBlbw55Q01zI%3D&Hidden1=&__VIEWSTATEGENERATOR=189D346C
    

And this is the script making the request, I'm sorry if it's so messy, just
need something quick.

    
    
    import requests
    import bs4
    import urllib.parse
    def main():
        session = requests.Session()
        headers = {"Origin": "https://www.wes.org",
                   "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                   "Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1", "Connection": "close",
                   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
                   "Referer": "https://www.wes.org/appstatus/indexca.aspx", "Accept-Encoding": "gzip, deflate, br",
                   "Accept-Language": "en-US,en;q=0.8,fa;q=0.6", "Content-Type": "application/x-www-form-urlencoded"}
        r = session.get('https://www.wes.org/appstatus/index.aspx',headers=headers)
        cookies = r.cookies
        soup = bs4.BeautifulSoup(r.content, "html5lib")
        viewState=urllib.parse.quote(str(soup.select('#__VIEWSTATE')[0]).split('value="')[1].split('"/>')[0])
        viewStateGenerator=urllib.parse.quote(str(soup.select('#__VIEWSTATEGENERATOR')[0]).split('value="')[1].split('"/>')[0])
        eventValidation=urllib.parse.quote(str(soup.select('#__EVENTVALIDATION')[0]).split('value="')[1].split('"/>')[0])
        paramsPost = {}
        paramsPost.update({'__VIEWSTATE':viewState})
        paramsPost.update({'__VIEWSTATEGENERATOR':viewStateGenerator})
        paramsPost.update({'__EVENTVALIDATION':eventValidation})
        paramsPost.update({"txtUID": "My@Email.Removed"})
        paramsPost.update({"txtPWD": "My_So_Called_Password"})
        paramsPost.update({"Submit": "Log In"})
        paramsPost.update({"Hidden1": ""})
        response = session.post("https://www.wes.org/appstatus/index.aspx", data=paramsPost, headers=headers,
                                cookies=cookies)
        print("Status code:", response.status_code) #Outputs 500.
        #print("Response body:", response.content)
    
    
    if __name__ == '__main__':
        main()
    

Any help would be so much appreciated.


Answer: You are doing way too much work and in doing so not passing valid data,you
extract value attribute directly i.e
`.select_one('#__VIEWSTATEGENERATOR')["value"]` and the same for all the rest,
the cookies will be set in the Session object after your initial get so the
logic boils down to:

    
    
    with requests.Session() as session:
            headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"}
            r = session.get('https://www.wes.org/appstatus/index.aspx', headers=headers)
    
            soup = bs4.BeautifulSoup(r.content, "html5lib")
            viewState = soup.select_one('#__VIEWSTATE')["value"]
            viewStateGenerator = soup.select_one('#__VIEWSTATEGENERATOR')["value"]
            eventValidation = soup.select_one('#__EVENTVALIDATION')["value"]
            paramsPost = {'__VIEWSTATE': viewState,'__VIEWSTATEGENERATOR': viewStateGenerator,
                          '__EVENTVALIDATION': eventValidation,"txtUID": "My@Email.Removed",
                        "txtPWD": "My_So_Called_Password",
                          "Submit": "Log In","Hidden1": ""}
            response = session.post("https://www.wes.org/appstatus/index.aspx", data=paramsPost, headers=headers)
            print("Status code:", response.status_code)
    

Python by convention uses CamelCase for class names and lowercase with
underscores to separate multiple words, you might want to consider applying
that to your code.

