2017-05-04 5 views
3

Ich versuche, eine HTTP-Anforderung in Swift3, um POST 2-Parameter auf eine URL auszuführen.HTTP-Anforderung in Swift mit POST-Methode in swift3

Beispiel:

-Link:

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

Params:

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

Was ist der einfachste Weg, das zu tun?

Ich möchte nicht einmal die Antwort lesen. Ich möchte nur senden, um Änderungen an meiner Datenbank durch eine PHP-Datei durchzuführen.

+0

Was haben Sie bisher versucht, welche Frameworks verwenden Sie? Haben Sie zumindest versucht, nach etwas wie * http rest client * zu suchen? – flob

Antwort

5

Ich denke, Sie total Neuling sind, aber hier ist etwas, das Sie in SWIFT 3 versuchen:

  1. öffnen info.plist Datei (Doppelklick oder Rechtsklick> Öffnen als> Source Code
  2. )
  3. diesen Code einfügen vor dem Schließen </dict> und </plist> tags:

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

  4. Jetzt ist Ihre Ansicht-Controller öffnen und diesen Code einfügen, wo Sie Post-Anforderung machen wollen:

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

Hinweis: Sie können print-Anweisungen entfernen, wenn Sie nicht brauchen!

Hoffe, das hilft! :)

+0

Arbeitete für mich !! Danke :) – 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

Sie müssen User558 Weg, wenn Sie irgendwelche HTML-Entities haben! Dann, wenn Sie es an eine PHP-Anwendung senden, müssen Sie etwas tun: https://Stackoverflow.com/a/18867369/1839484 – Ohiovr