swift ios 移动应用程序登录提交 [英] swift ios mobile app login submit

查看:32
本文介绍了swift ios 移动应用程序登录提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我们大学的这个网站编写一个 iOS 应用程序:https://uniworx.ifi.lmu.de/

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

我登录失败.我已经设法为登录表单发送一个 http 提交,但我总是得到相同的站点作为响应.我想访问您通常在登录后看到的网站.我做错了什么?

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?

这是我的函数代码:

 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-.

推荐答案

试试这个,希望对你有帮助!!

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

在 Swift 2+ 中

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()

在 Swift 3 中你可以

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()

这篇关于swift ios 移动应用程序登录提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆