Getting Header Response in Swift

前端 未结 2 1559
猫巷女王i
猫巷女王i 2021-02-14 13:12

I am following this answer for making HTTP calls in my swift project. How to make an HTTP request in Swift?

and following is the code I am using to make a synchronous

2条回答
  •  执念已碎
    2021-02-14 13:53

    Per your code in the original question, have you tried this?

    let urlPath: String = "http://apiserver.com/api/login/?username=asdf&password=asdf"
    var url: NSURL = NSURL(string: urlPath)!
    var request1: NSURLRequest = NSURLRequest(URL: url)
    
    var response: NSURLResponse? = nil
    
    var error: NSError? = nil
    var dataVal: NSData =  NSURLConnection.sendSynchronousRequest(request1, returningResponse: &response, error:&error)!
    var err: NSError
    
    println("response -- \(response)")
    if let response = response as? NSHTTPURLResponse {
        if response.statusCode == 200 {
            print("Success")
        }
    }
    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
    
    println("Synchronous \(jsonResult)")
    

提交回复
热议问题