2016-10-22 183 views
1

我想用用戶名和密碼從服務器獲取認證密鑰發送請求,這裏是我的代碼,我不知道如何用請求發送給參數,請幫我解決它。HTTP在Swift中發送請求參數

let urlString = "http://services.84069.ir/Restful/PaymentService.svc/authenticate" 
func recentProfileURL(parameter : [String:String]?) -> NSURL { 

    let component = NSURLComponents(string: urlString)! 
    var queryItem = [NSURLQueryItem]() 
    if let param = parameter { 
     for (key,value) in param { 
      let item = NSURLQueryItem(name: key, value: value) 
      queryItem.append(item) 
     } 
    } 
    component.queryItems = queryItem 
    return component.URL! 
} 

func fetchCode(completion completion: (ProfileResult) -> Void){ 

    let request = NSURLRequest(URL: recentProfileURL(["UserName": self.userNameParam])) 

    let task = session.dataTaskWithRequest(request, completionHandler: { 
     (data, response, error) in 

     let result = self.processUserProfileRequest(data: data, error: error) 
    completion(result) 
    }) 
    task.resume() 
} 

我試圖爲獲得認證碼從服務器如何可以發送參數的陣列,而不是一個參數發送用戶名和密碼?

+0

試試這個也許這將是對您有用! http://stackoverflow.com/questions/40108462/swift-ios-mobile-app-login-submit/40108876#40108876 –

回答

0

這應該爲你工作:

var request = NSMutableURLRequest(URL: NSURL(string: ""http://services.84069.ir/Restful/PaymentService.svc/authenticate")) 

var session = NSURLSession.sharedSession() 
request.HTTPMethod = "POST" 

var params = ["UserName":"UserName you wish to send", "password":"Password you wish to send"] as Dictionary<String, String> 

var err: NSError? 

request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err) 

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
    print("Response: \(response)")` 

} 
+0

我使用你的建議時有一個這個錯誤:JSON文本沒有開始數組或對象和選項允許片段未設置。 – ava

0

試試這個,我希望這將是對你有幫助!

let request = NSMutableURLRequest(URL: NSURL(string: "http://example.com/login.php")!) 
request.HTTPMethod = "POST" 
let postString = "Username=Yourusername&password=Yourpassword" 
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in 
    guard error == nil && data != nil else {               // check for fundamental networking error 
     print("error=\(error)") 
     return 
    } 

    if let httpStatus = response as? NSHTTPURLResponse where 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: NSUTF8StringEncoding) 
    print("responseString = \(responseString)") 
} 
task.resume() 
+0

我的反應應該是{ 「錯誤碼」 0 「的ErrorMessage」: 「」, 「結果」:1, 「AUTHKEY」: 「50_7476070793275070744414070707641070724131070235735070714812070238280701410440702975070464736070567130070702745070137545070442569070243973070」},但這個建議拋出anerror – ava

+0

@ava檢查您的網址是正確與否? –

+0

這個結果是JSON類型的嗎?在safari中輸入url時顯示以下消息:方法不允許。 – ava

0

試試這個代碼

let request = NSMutableURLRequest(URL: NSURL(string: "http://www.thisismylink.com/postName.php")!) 
 
request.HTTPMethod = "POST" 
 
let postString = "id=13&name=Jack" 
 
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
 
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in 
 
    guard error == nil && data != nil else {               // check for fundamental networking error 
 
     print("error=\(error)") 
 
     return 
 
    } 
 

 
    if let httpStatus = response as? NSHTTPURLResponse where 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: NSUTF8StringEncoding) 
 
    print("responseString = \(responseString)") 
 
} 
 
task.resume()