2017-05-04 86 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