我正在尝试在Swift3中运行HTTP请求,以将2个参数发布到URL。

示例:

链接:

http://test.tranzporthub.com/street45/customer_login.php


参数:

{
    "user_id":"chaitanya3191@gmail.com",
    "password":"123"
}


最简单的方法是什么?

我什至不想阅读回复。我只想发送该文件,以通过PHP文件对数据库进行更改。

评论

到目前为止,您尝试了什么?您使用的是什么框架?您至少尝试过搜索类似http rest client的东西吗?

#1 楼

我想,您完全是新手,但是您可以在SWIFT 3中尝试以下操作:


打开info.plist文件(双击或右键单击>打开方式>源代码)

在关闭</dict></plist>标签之前粘贴此代码:

<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
    <key>tranzporthub.com</key>
    <dict>
        <!--Include to allow subdomains-->
        <key>NSIncludesSubdomains</key>
        <true/>
        <!--Include to allow HTTP requests-->
        <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
        <true/>
        <!--Include to specify minimum TLS version-->
        <key>NSTemporaryExceptionMinimumTLSVersion</key>
        <string>TLSv1.1</string>
    </dict>
</dict>






现在打开视图控制器并将此代码粘贴到您要发出请求的位置:

var request = URLRequest(url: URL(string: "http://test.tranzporthub.com/street45/customer_login.php")!)
request.httpMethod = "POST"
let postString = "user_id=chaitanya3191@gmail.com&password=123"
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()



注意:如果不需要,您可以删除打印语句!

希望有帮助! :)

评论


为我工作!谢谢 :)

– iUser
17年7月11日在13:27

#2 楼

 @IBAction func postAction(_ sender: Any) {
    let Url = String(format: "your url")
    guard let serviceUrl = URL(string: Url) else { return }
    //        let loginParams = String(format: LOGIN_PARAMETERS1, "test", "Hi World")
    let parameterDictionary = ["username" : "@kilo_laco", "tweet" : "Hi World"]
    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {
        return
    }
    request.httpBody = httpBody

    let session = URLSession.shared
    session.dataTask(with: request) { (data, response, error) in
        if let response = response {
            print(response)
        }
        if let data = data {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                print(json)
            }catch {
                print(error)
            }
        }
        }.resume()


}


评论


如果您有任何html实体,就需要User558的方式!然后,如果将其发送到php应用程序,则必须执行以下操作:stackoverflow.com/a/18867369/1839484

– Ohiovr
17年8月27日在6:43