How to login to a site using POST request Swift iOS

0 votes

I want to create an iOS Application that logs into a website and parses the data from several pages of that site, while maintaining the login session.This is what I have done so far. I send a GET request to retrieve the EVENTVALIDATON and VIEWSTATE parameters required for the POST request. (I looked at the POST by using 'Firebug'). When I run the following code, it gives back the same login page. But it should be giving 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'm very new to requests and parsing data(I have finished the parsing part separately though). I did some more research and read about headers and cookies.So after checking the headers, the initial GET request by the browser has a response header of

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 of

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 a initial GET request can have a token with it. If request happens first, response should be the one containing the token? I don't know what I am doing wrong and how to get this working. I don't know if I am missing something altogether. I came here only after trying everything I could think of. Any help would be appreciated. Thank you.

Nov 9 in Mobile Development by gaurav
• 22,040 points
42 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Mobile Development

0 votes
0 answers
0 votes
0 answers
0 votes
0 answers

Change English Number To Persian Numbers in Text Swift,IOS

Hi How can i replace english numbers ...READ MORE

Nov 8 in Mobile Development by gaurav
• 22,040 points
31 views
0 votes
0 answers

How to receive single tap and hold Remote-Control Event with iOS

I'm looking for my app to listen ...READ MORE

Nov 8 in Mobile Development by gaurav
• 22,040 points
40 views
0 votes
0 answers

How to use iOS Reachability

I'm developing an iPhone app that uses ...READ MORE

Nov 9 in Mobile Development by gaurav
• 22,040 points
30 views
0 votes
0 answers

How to draw heart shape in UIView (iOS)?

I want to create different shapes. Below ...READ MORE

Nov 10 in Mobile Development by gaurav
• 22,040 points
26 views
0 votes
1 answer

iOS App : Porting Code to App

If you make a post, missing parameters ...READ MORE

answered Jan 18, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
239 views
0 votes
1 answer

Get User Group in Swift using AWS Cognito

The groups are exposed in the ID ...READ MORE

answered Nov 12, 2018 in AWS by Priyaj
• 58,100 points
961 views
0 votes
1 answer

URL Scheme for iOS Home App

I think you could try doing it ...READ MORE

answered Nov 16, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
500 views
0 votes
1 answer

swift for windows? xcode on windows? ?

Hi, @Aminegac, If you’re a little more technically ...READ MORE

answered Dec 4, 2020 in Others by Nikita
693 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP