0
我想使用基本身份驗證發起http請求。我試圖遵循這個主題:click here。但是Xcode的告訴我之後的HttpResponse使用基本身份驗證發起http請求
錯誤的請求,我不知道爲什麼從今天早晨起。也許任何人有一個比我能在網上找到的更有趣的想法? :)
這裏是我的代碼:
func topRefresh(sender:AnyObject){ var list=Array<Notification>() //credential let credentialLogin = NSUserDefaults.standardUserDefaults().objectForKey("login") as! String let credentialPassword = NSUserDefaults.standardUserDefaults().objectForKey("password") as! String // set up the base64-encoded credentials let loginString = NSString(format: "%@:%@", credentialLogin, credentialPassword) let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)! let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) // create the request let url = NSURL(string: jsonLink)! let request = NSMutableURLRequest(URL: url) request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization") request.HTTPMethod="GET" let paramString="login="+credentialLogin request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in let httpResponse = response as! NSHTTPURLResponse if (httpResponse.statusCode == 200) { do{ if (data != nil){ self.notificationsDisplayed.removeAll() let jsonDict = try NSJSONSerialization.JSONObjectWithData(data!,options: .AllowFragments) list=self.parseJson(jsonDict) if (self.notifications.count==0){ self.notifications=self.copyArray(list) list.removeAll() }else{ //compare new data with past data while(list.count>0){ print(list.count) let tmp=list.last for notification in self.notifications { if(tmp!.id==notification.id){ list.removeLast() break }else{ self.notifications.insert(tmp!, atIndex: 0) list.removeLast() break } } } } self.orderByDate(&self.notifications) self.notificationsDisplayed=self.copyArray(self.notifications) self.applyFilter() print("Data parsed") }else{ print("Data is empty") } }catch { print("Error with Json: \(error)") } }else{ print("HTTP Error") } self.refreshControl?.endRefreshing() print("Finished") } task.resume() }
我怎麼能在我的實際使用此功能代碼? –
我必須使用會話而不是請求來驗證用戶嗎? –