2017-05-04 132 views
3

我试图在Swift3中运行HTTP请求,将POST 2参数发送到URL。Swift中使用POST方法的Swift中的HTTP请求

例子:

链接:

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

PARAMS:

{ 
    "user_id":"[email protected]", 
    "password":"123" 
} 

什么是做到这一点的最简单的方法?

我甚至不想阅读回复。我只是想通过一个PHP文件来发送数据库上的更改。

+0

你到目前为止尝试过什么,你在使用什么框架?你至少试过寻找类似* http rest client *的东西吗? – flob

回答

5

我猜,你是完全的新手,但这里的东西,你可以在SWIFT 3尝试:

  1. 打开的info.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> 
    

    :)

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

  3. 现在打开你的视图控制器并粘贴此代码在那里你想POST请求:

    var request = URLRequest(url: URL(string: "http://test.tranzporthub.com/street45/customer_login.php")!) 
    request.httpMethod = "POST" 
    let postString = "[email protected]&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() 
    

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

希望这会有所帮助! :)

+0

为我工作!谢谢:) – iUser

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


} 
+0

你需要它User558的方式,如果你有任何的HTML实体!然后,如果你把它发送到一个PHP应用程序,你必须做这样的事情:https://stackoverflow.com/a/18867369/1839484 – Ohiovr