【问题标题】:HTTP Request in Swift with POST method in swift3Swift3中使用POST方法的HTTP请求
【发布时间】:2017-10-02 09:51:14
【问题描述】:

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

例子:

链接:

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

参数:

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

最简单的方法是什么?

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

【问题讨论】:

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

标签: xcode swift3 httprequest postman raw-data


【解决方案1】:

我猜,您完全是新手,但您可以在 SWIFT 3 中尝试以下方法:

  1. 打开 info.plist 文件(双击或右键单击 > 打开为 > 源代码)
  2. 在关闭</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>
    

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

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

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

希望这会有所帮助! :)

【讨论】:

  • 为我工作!!谢谢你:)
【解决方案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
猜你喜欢
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 2023-04-02
  • 2021-03-31
相关资源
最近更新 更多