2016-10-22 186 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()