2016-01-27 55 views
0

我正在使用Objective-C語言,並且需要從Objective-C中打開卷曲。如何將我的捲曲轉換爲Objective-C

我捲曲:

curl https://api.start.payfort.com/tokens/ \ 
    -u test_open_k_91ffe6d8f9efe14fcc91: \ 
    -d "number=4242424242424242" \ 
    -d "exp_month=11" \ 
    -d "exp_year=2016" \ 
    -d "cvc=123" \ 
    -d "name=sapana" 

請幫助我如何這個URL轉換在Objective-C和消防網址。

我嘗試了一些其他的方式,但他們返回響應,如:

{ 
    error =  { 
     code = "not_found"; 
     extras =   { 
      "request_id" = "f8e70bf6-9391-49b0-9403-06cc9485c021"; 
      time = "2016-01-27T11:10:31Z"; 
     }; 
     message = "Not Found"; 
     type = processing; 
    }; 
} 
+0

請閱讀[如何提出好的問題(// stackoverflow.com/help/how-to-ask),並嘗試編輯你的問題。另外,請修改您的期望以及該過程。 **堆棧溢出不是免費的代碼寫入服務**。看看'NSURLRequest'和相關的文檔。謝謝! – SmokeDispenser

回答

1

這是爲我工作。但迅速

func cardSendToPayFort(number:String, exp_month:String, exp_year:String, cvc:String, name:String, completion: ((success: Bool, json: NSDictionary?) -> Void)) 
{ 

    let username = "test_open_k_91ffe6d8f9efe14fcc91" 
    let password = "" 
    let loginString = NSString(format: "%@:%@", username, password) 
    let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)! 
    let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) 

    let orderURL = NSURL(string: "https://api.start.payfort.com/tokens/") 
    let request = NSMutableURLRequest(URL: orderURL!) 

    request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization") 

    let postString = "number=\(number)&exp_month=\(exp_month)&exp_year=\(exp_year)&cvc=\(cvc)&name=\(name)" 
    //let postString = "number=4242424242424242&exp_month=11&exp_year=2016&cvc=123&name=sapana" 

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
    request.HTTPMethod = "POST" 

    let session = NSURLSession.sharedSession() 

    let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in 

     if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 201 { 
      print("statusCode should be 201, but is \(httpStatus.statusCode)") 
      print("response = \(response)") 

      dispatch_async(dispatch_get_main_queue()){ 
       completion(success: false, json: nil) 
      } 

     } else { 
      let responseString = String(data: data!, encoding: NSUTF8StringEncoding) 

      let jsonData = responseString?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) 
      let json = try! NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary 

      dispatch_async(dispatch_get_main_queue()){ 
       completion(success: true, json: json) 
      } 
     } 

    } 
    task.resume() 
}