Tag: nsurlsession

发送http发布请求时,NSURLErrorDomain代码= -1001错误

我正在使用下面的代码发布图像到我的服务器。 @IBAction func postButtonPressed(sender: UIButton) { let task = NSURLSession.sharedSession().dataTaskWithRequest(createRequest("http://xx.xx.xxx.xxx/xxxx/"), completionHandler: { data, response, error in println(NSString(data: data, encoding: NSUTF8StringEncoding)) }) task.resume() } createRequest()创build所需的NSURLRequest对象。 当我使用模拟器时,这工作正常 。 问题是当我在iPhone上运行我的应用程序时,出现以下错误。 Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn't be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x155e71f0 {NSErrorFailingURLKey=http://xx.xxx.xxx.xxx/xxxx/, NSErrorFailingURLStringKey=http://54.148.156.117/query/, NSUnderlyingError=0x155674d0 "The operation couldn't be completed. (kCFErrorDomainCFNetwork error -1001.)"} 我已经知道这个错误是一个超时错误。 重新启动应用程序或电话没有帮助。 此外,我试图从Web浏览器发布图像到我的服务器,它没有问题。 什么可能导致这个超时错误? 编辑:当我监视我的networking活动,我意识到应用程序发送10 […]

里面的dataTaskWithRequest不是执行。 在Swift中打开API

当我用POSTMAN打相同的数据我得到所需的响应。 但在我的函数内dataTaskWithRequest我没有得到任何回应。 func sendData (){ let url = NSURL(string: "http://www.example.com/quiz-school/mobileData/request.php?request=QuizTotal&module=PQ")! let request = NSMutableURLRequest(URL: url) request.HTTPMethod = "POST" request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions()) request.setValue("application/json", forHTTPHeaderField: "Content-Type") let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in if(error != nil){ print(error) return } ((response as! NSHTTPURLResponse).statusCode) } task.resume() } 在数据任务 数据任务后 邮递员回复 谢谢任何帮助将不胜感激。 我正在发送的字典 { data = […]

使用iOS8中的NSUrlSession使用stream请求进行背景上传

之前在iOS7中,当我们尝试在后台使用stream请求上传时,我们得到以下exception 终止应用程序,由于未捕获的exception'NSGenericException',原因:'在后台会话中上传任务必须来自文件' 但是在iOS8中,我们尝试在后台使用stream上传时没有出现exception。 现在我的问题是 1)是不是上传与uploadTaskWithStreamedRequest:允许在iOS8? 2)在iOS8中,我使用了带有uploadTaskWithStreamedRequest的后台NSURLConfiguration。 我正在使用– (void)URLSession:(NSURLSession *)会话任务:(NSURLSessionTask *)任务needNewBodyStream:(void(^)(NSInputStream *))completionHandler提供stream到NSUrlSession。 当应用程序在前台它工作正常,上传我的文件到服务器。 但是一旦应用程序在后台启动,stream将结束,并且NSURLSession完成,并显示以下错误消息 错误域= NSURLErrorDomain代码= -997“丢失连接到后台传输服务” 我认为当应用程序在后台我的stream结束。 现在我的问题是,我应该在哪些runloop我应该安排我的stream,或让我知道如果我的理解有任何错误。 -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *))completionHandler { // Open producer/consumer streams. We open the producerStream straight // away. We leave the consumerStream alone; NSURLConnection will deal // with it. NSLog(@"%@", [NSThread currentThread]); NSInputStream *consStream; NSOutputStream *prodStream; […]

NSURLSession内存泄漏

即使使NSURLSession失效,使用Instruments运行configuration文件,一些名为TubeManager的类(可能是私有的),HTTPConnectionCache和HTTPConnectionCacheDictionary在内存中仍然存在。 代码片段重现: NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession* session = [NSURLSession sessionWithConfiguration:config]; NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]; NSURLSessionDataTask* sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { [session finishTasksAndInvalidate]; }]; [sessionDataTask resume];

我如何使iOS的caching无效的特定url?

使用NSURLSession的默认caching,如何使特定URL的caching无效? 我注意到NSURLCache的removeCachedResponseForRequest:方法,但是这需要一个NSURLRequest对象,我没有原始请求。 我是否需要在创build它们时存储这些内容,以便将它们传回到removeCachedResponseForRequest:或者,我可以使用相应的URL创build一个新的URL,然后将其作为等效目的,即使它没有与原始相同的头域和其他属性?

获取Apple Watch的服务器数据

我想知道如何去devise一个watchOS应用程序,取决于从服务器获取最新的饲料。 您是否需要使用应用程序上下文,并让iPhone使用WatchConnectivity框架将其推到后台,还是在Apple Watch上使用NSURLSession? 请记住这是为了watchOS 2。

IOS使用NSURLSession FTP上传

我正试图通过FTP上传文件到服务器。 根据Apple文档 NSURLSession类支持FTP操作。 有一个着名的苹果开发者博客也支持。 但是还不清楚NSURLSession API是否支持ftp上传? (我尝试了build议的方式,并得到错误)。 使用CFFTPStreamRef的传统方法,ftp上传工作正常,但在9.0中不推荐使用。 头文件说: CF_DEPRECATED(10_3,10_11,2_0,9_0,“使用NSURLSessionAPI的FTP请求”) 任何想法,例子或链接获得帮助。 我正在尝试这样的事情: NSURL *url_upload = [NSURL URLWithString:@"ftp://username:password@thelink/myfolder/filename.zip"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_upload]; [request setHTTPMethod:@"PUT"]; NSURL *docsDirURL = [NSURL fileURLWithPath:filePath]; NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url_upload.host port:[url_upload.port integerValue] protocol:url_upload.scheme realm:nil authenticationMethod:nil]; NSURLCredential *cred = [NSURLCredential credentialWithUser:userId password:password persistence:NSURLCredentialPersistenceForSession]; NSURLCredentialStorage * cred_storage ; [cred_storage setCredential:cred […]

Xcode 7.1在iOS 9中NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813)

我正在使用iOS 9作为目标和使用Xcode 7.1,尝试一切,因为我的水平最好的ATS的所有解决scheme,但没有奏效。 以下是控制台中的以下错误说明。 如果有任何build议,我将被迫。 这里的一些关键的东西是 – NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) Error excluding Main.db from backup Error Domain=NSCocoaErrorDomain Code=4 "The file “Main.db” doesn't exist." UserInfo={NSURL=file:///Users/Raj/Library/Developer/CoreSimulator/Devices/BC3A0589-3B9A-4AFD-8F2A-B1C92FA341DD/data/Containers/Data/Application/70B3043A-8553-41E5-A147-8508D08BF3E6/Documents/Main.db, NSFilePath=/Users/Raj/Library/Developer/CoreSimulator/Devices/BC3A0589-3B9A-4AFD-8F2A-B1C92FA341DD/data/Containers/Data/Application/70B3043A-8553-41E5-A147-8508D08BF3E6/Documents/Main.db, NSUnderlyingError=0x798465c0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 2016-08-30 21:09:10.867 GFIM[80653:484899] SFOAuthCredentials:revokeRefreshToken: refresh token revoked. Cleared identityUrl, instanceUrl, issuedAt fields 2016-08-30 21:09:10.871 GFIM[80653:484899] INFO|0|SFPasscodeManager|Resetting passcode upon […]

由于iOS 10中的nonce-count错误,HTTP摘要authentication失败

由于NSURLSession生成的Authorization:Digest头中存在错误的nonce-count,自从iOS 10以来,HTTP摘要authentication不再适用于我们的应用程序。 相同的代码适用于iOS 9,但无法在iOS 10中进行authentication 用NSURLRequest创build一个POST请求 用NSURLSession启动它 在urlSession(_:didReceive:completionHandler:) delegate中处理NSURLAuthenticationMethodHTTPDigest 服务器按照预期响应401和qop =“auth”string 应用程序再次请求授权:摘要标题集。 根据RFC2617 : 随机数数 如果发送了一个qop指令(见上),就必须指定这个参数,如果服务器没有在WWW-Authenticate头域中发送一个qop指令,就不能指定。 nc-value是客户端在此请求中使用nonce值发送的请求数(包括当前请求)的hex计数。 例如,在响应给定的nonce值发送的第一个请求中,客户端发送“nc = 00000001”。 该指令的目的是允许服务器通过维护自己的计数副本来检测请求重放 – 如果相同的nc值被看到两次,则该请求是重放。 请参阅下面关于request-digest值构造的描述。 然而,即使对于iOS 10中的第一个请求,nonce-count从“nc = 00000002”开始,这导致服务器拒绝它。 预计服务器响应200确定 iOS 9和之前: POST /Tunnel/Message.aspx HTTP/1.1 Host: 172.18.70.12:3454 Accept: */* Content-Type: application/xml User-Agent: iViewer/1 CFNetwork/758.5.3 Darwin/15.6.0 Connection: keep-alive Cookie: AuthType: digest Accept-Language: zh-tw Content-Length: 69 Accept-Encoding: gzip, […]

在Swift中用NSURLSession下载一个文件

编译器说,我有2个问题在这里,首先我不能设置NSURLSessionDownloadDelegate一个快速的项目 Type 'ViewController' does not conform to protocol 'NSURLSessionDownloadDelegate' 第二个问题是我找不到NSURLSession方法来下载一个简单的文件 这里是我用来下载简单文件的方式 var url:NSURL = NSURL.URLWithString(fileURL) var request:NSURLRequest = NSURLRequest(URL: url) var downloadTask:NSURLSessionDownloadTask = sessionManager.downloadTaskWithRequest(request) downloadTask.resume() 而这些是我想迅速做出的方法 URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite .. URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location .. URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error ..如果有一种新的方式来下载文件NSURLSession我想知道,什么新的NSURLSession在迅速