Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift3在POST登录后提交POST表单_Ios_Python 3.x_Post_Swift3 - Fatal编程技术网

Ios Swift3在POST登录后提交POST表单

Ios Swift3在POST登录后提交POST表单,ios,python-3.x,post,swift3,Ios,Python 3.x,Post,Swift3,我一直在尝试制作一个简单的应用程序来打开我工作时的RFID门。这是一个非常简单的网站,一个POST登录(带有用户名和pwd),然后另一个POST表单提交要打开的门。因此,我在Python3中创建了一个脚本,它可以完美地完成此任务: import requests import collections payload = collections.OrderedDict() payload['username'] = 'admin' payload['pwd'] = 'password' payl

我一直在尝试制作一个简单的应用程序来打开我工作时的RFID门。这是一个非常简单的网站,一个POST登录(带有用户名和pwd),然后另一个POST表单提交要打开的门。因此,我在Python3中创建了一个脚本,它可以完美地完成此任务:

import requests
import collections

payload = collections.OrderedDict()
payload['username'] = 'admin'
payload['pwd'] = 'password'
payload['logId'] = '20101222'  

payload2 = collections.OrderedDict()
payload2['UNCLOSE1'] = 'Remote Open #1 Door Front Door'

payload3 = collections.OrderedDict()
payload3['s6'] = 'Exit'

with requests.Session() as s:
    p = s.post('http://192.168.99.25/ACT_ID_1', data=payload)
    print(p.text)
    print()

    r = s.post('http://192.168.99.25/ACT_ID_701', data=payload2)
    print(r.text)
    print()

    r2 = s.post('http://192.168.99.25/ACT_ID_21', data=payload3)
    print(r2.text)
这就是我在Python3中的代码,现在我一直在尝试将其移植到swift3(在应用程序中这对我来说会更容易,除非有一种可以接受的方式来运行python文件)

我一直在使用URLSession,但我还没有使用它的经验。如果你想要我一直试图使用的代码,我可以发布它,但它相当垃圾

谢谢-亚舍

编辑:

我一直在尝试的代码:

func connectToRFID(_ session: URLSession) {

    let addressOne = URL(string: "http://192.168.99.25/ACT_ID_1")!
    let postOne = "username=admin&pwd=password&logId=20101222"

    var request = URLRequest(url: addressOne)


    request.httpMethod = "POST"
    request.httpBody = postOne.data(using: .utf8)
    let task = session.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {                                                 // check for fundamental networking error
            print("error = \(String(describing: 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 = \(String(describing: response))")
        }

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

func openRFID(_ session: URLSession) {
    let addressTwo = URL(string: "http://192.168.99.25/ACT_ID_701")!
    let postTwo = "UNCLOSE1=Remote Open #1 Door Front Door"
    var request2 = URLRequest(url: addressTwo)

    request2.httpMethod = "POST"
    request2.httpBody = postTwo.data(using: .utf8)

    let task2 = session.dataTask(with: request2) { data, response, error in
        guard let data = data, error == nil else {
            print("error = \(String(describing: error))")
            return
        }
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(String(describing: response))")
        }

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

    }
    task2.resume()
}

@IBAction func buttonPress(_ sender: Any) {
    let session = URLSession.shared
    connectToRFID(session)
    print("One")
    openRFID(session)
}

我也看过其他几个帖子,但大多数都只告诉我如何提交帖子。顺便说一句,
connectorfid
函数确实登录,(它响应登录后的页面),但是
openRFID
在控制台中不返回任何内容。

URLSession任务异步工作。所以不能在主线程中逐行调用它们。你需要等待回复。就你而言:

登录任务完成后,需要调用openRFID函数

func connectToRFID(_ session: URLSession) {

    let addressOne = URL(string: "http://192.168.99.25/ACT_ID_1")!
    let postOne = "username=admin&pwd=password&logId=20101222"

    var request = URLRequest(url: addressOne)

    request.httpMethod = "POST"
    request.httpBody = postOne.data(using: .utf8)
    let task = session.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {                                                 // check for fundamental networking error
            print("error = \(String(describing: 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 = \(String(describing: response))")
        }
        else {
            self.openRFID(URLSession.shared)
            let responseString = String(data: data, encoding: .utf8)
            print("responseString = \(String(describing: responseString))")
            print()
        }
        let responseString = String(data: data, encoding: .utf8)
        print("responseString = \(String(describing: responseString))")
        print()
    }
    task.resume()
}

您应该首先对问题进行研究,然后先尝试,然后来这里询问您的问题,发布相关代码库,告诉详细信息等。您无法获得这些帮助。@UlasSancak在那里我添加了Swift3代码,我为没有更具体表示歉意。我已经研究过这个问题,但是我发现没有一篇帖子对我有帮助(是的,我尝试过将解决方案组合在一起)。谢谢,我过一会儿就试试这个,我正在吃午饭。但这完全有道理,URLSession打开了一个新的“线程”(Python术语),并在代码中继续,基本上。。。。我想,我会这么想的,谢谢,不客气。事实上,我一看到它就告诉你了。我不知道是否还有其他问题。如果有,请告诉我。还要记住一些事情。响应块位于主线程上,如果您想基于响应数据显示一些数据,您应该使用DispatchQueue.main.async{//call UI elements}手动调用主线程上的UI元素,非常感谢!当我听到门打开时,我真的跳了起来,哈哈。我很乐意帮忙。:)