0

I'm trying to program an iOS application for this website of our university: https://uniworx.ifi.lmu.de/

I failed with the login. I have managed it to send a http submit for the login form, but I always get the same site back as response. I want to get the site you normally see after logging in. What am I doing wrong?

Here is the code of my function:

 func newTest()
{
    let request = NSMutableURLRequest(url: NSURL(string: "http://uniworx.ifi.lmu.de/?action=uniworxLoginDo")! as URL)
    request.httpMethod = "POST"
    let postString = "username=myusername&password=mypassword"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
        guard error == nil && data != nil else {                                                          
            print("error=\(error)")
            return
        }

        if let httpStatus = response as? HTTPURLResponse , httpStatus.statusCode != 200 {          
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = String(data: data!, encoding: String.Encoding.utf8)
        print("responseString = \(responseString)")


    }
    task.resume()




}

I've changed my real username and password in the code -of course-.

1
  • Did you try to check your url if it is valid?
    – P. Pawluś
    Oct 18, 2016 at 12:44

1 Answer 1

1

Try this I hope it would be helpful for you!!

In Swift 2+

let request = NSMutableURLRequest(URL: NSURL(string: "http://uniworx.ifi.lmu.de/?action=uniworxLoginDo")!)
    request.HTTPMethod = "POST"
    let postString = "Username=Yourusername&password=Yourpassword"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
        guard error == nil && data != nil else {                                                          // check for fundamental networking error
            print("error=\(error)")
            return
        }

        if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = \(responseString)")
    }
    task.resume()

In Swift 3 You can

var request = URLRequest(url: URL(string: "http://uniworx.ifi.lmu.de/?action=uniworxLoginDo")!)
request.httpMethod = "POST"
let postString = "Username=Yourusername&password=Yourpassword"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data, error == nil else {                                                 // check for fundamental networking error
        print("error=\(error)")
        return
    }

    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
        print("statusCode should be 200, but is \(httpStatus.statusCode)")
        print("response = \(response)")
    }

    let responseString = String(data: data, encoding: .utf8)
    print("responseString = \(responseString)")
}
task.resume()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.