How do I log into a site using a POST request? (Swift, IOS)

I want to create an iOS app that logs into a website and parses data from multiple pages of that site while keeping the login session.This is what I have done so far. I am sending a GET request to retrieve the EVENTVALIDATON and VIEWSTATE parameters required for the POST request. (I looked at POST using "Firebug"). When I run the following code, it returns the same login page. But he has to give me this page .

    var parameter: Parameters = [:]
    var viewstate: String = ""
    var eventvalidation: String = 


    @IBAction func postRequest(_ sender: Any) {


    Alamofire.request("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx").responseString { response in
        print("\(response.result.isSuccess)")
        if let html = response.result.value {
            if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8) {
                // Search for nodes by CSS selector

                for show in doc.css("input[id='__VIEWSTATE']") {
                    self.viewstate=show["value"]!
                    //print(show["value"] as Any)
                }

                for show in doc.css("input[id='__EVENTVALIDATION']") {
                    self.eventvalidation=show["value"]!
                    //print(show["value"] as Any)
                }
            }
        }
        //creating dictionary for parameters
        self.parameter = ["__EVENTTARGET":"",
                         "__EVENTARGUMENT":"",
                         "__LASTFOCUS":"",
                         "__VIEWSTATE":self.viewstate,
                         "__EVENTVALIDATION":self.eventvalidation,
                         "rdolst":"S",
                         "Txtstudid":"<myrollno>",
                         "TxtPasswd":"<mypassword>",
                         "btnlogin":"Login"
        ]
    }
    Alamofire.request ("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx",method: .post, parameters: self.parameter, headers: headers).responseString { response in
        print("\(response.result.isSuccess)")
        print(response)
    }

      

To be honest, I am very new to queries and data analysis (I have partially ironed out some of the parsing). I did some more research and read about headers and cookies. Therefore, after checking the headers, the initial GET request by the browser has a response header

Cache-Control :   private
Content-Encoding :   gzip
Content-Length :   4992
Content-Type :   text/html; charset=utf-8
Date :   Sun, 18 Jun 2017 14:25:50 GMT
Server :    Microsoft-IIS/8.0
Set-Cookie :   .ASPXAUTH=; expires=Mon, 11-Oct-1999 18:30:00 GMT; path=/; HttpOnly
Vary :   Accept-Encoding
X-AspNet-Version :   4.0.30319
X-Powered-By :   ASP.NET

      

and request header

Accept :    text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8
Accept-Encoding :   gzip, deflate, br
Accept-Language :   en-US,en;q=0.5
Connection :   keep-alive
Cookie :   ASP.NET_SessionId=urzugt0zliwkmz3ab1fxx1ja
Host :   ecampus.psgtech.ac.in
Upgrade-Insecure-Requests :   1
User-Agent :   Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0`

      

The problem is I don't understand how the original GET request could have a token with it. If the request is executed first, should the response contain the one containing the token? I don't know what I am doing wrong and how to do it. I don't know if anything is missing at all. I only came here after trying everything I could think of. Any help would be greatly appreciated. Thank.

+3


source to share


1 answer


EVENTVALIDATON and VIEWSTATE parameters required for POST <-

But in your code, the POST request is executed immediately after the GET request, at this point the self.parameter is empty

Alamofire has an asynchronous completionHandler



Wait for the GET request to complete and send a POST request:

var parameter: Parameters = [:]
    var viewstate: String = ""
    var eventvalidation: String = 


@IBAction func postRequest(_ sender: Any) {


Alamofire.request("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx").responseString { response in
    print("\(response.result.isSuccess)")
    if let html = response.result.value {
        if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8) {
            // Search for nodes by CSS selector

            for show in doc.css("input[id='__VIEWSTATE']") {
                self.viewstate=show["value"]!
                //print(show["value"] as Any)
            }

            for show in doc.css("input[id='__EVENTVALIDATION']") {
                self.eventvalidation=show["value"]!
                //print(show["value"] as Any)
            }
        }
    }
    //creating dictionary for parameters
    self.parameter = ["__EVENTTARGET":"",
                     "__EVENTARGUMENT":"",
                     "__LASTFOCUS":"",
                     "__VIEWSTATE":self.viewstate,
                     "__EVENTVALIDATION":self.eventvalidation,
                     "rdolst":"S",
                     "Txtstudid":"15i231",
                     "TxtPasswd":"OpenSesame",
                     "btnlogin":"Login"
    ]

    //Wait for the GET request to complete, and then send the POST request: <<==
    Alamofire.request ("https://ecampus.psgtech.ac.in/studzone/AttWfLoginPage.aspx",method: .post, parameters: self.parameter, headers: headers).responseString { response in
        print("\(response.result.isSuccess)")
        print(response)
    }

}

      

+1


source







All Articles