NSURLConnection sendAsynchronousRequest - communicating with HTTPS

折月煮酒 提交于 2019-12-24 20:33:08

问题


Am working on an iPhone App which fetches an image from a URL. I was using 'NSData dataWithContentsOfUrl] & it worked fine. But then, as you might have guessed, the request is synchronous.

I want the request to be asynchronous. So, I tried using the NSURLConnection's sendAsynchronousRequest() call. But this returns the following error in the method 'didFailWithError' :

Error Domain=kCFErrorDomainCFNetwork Code=310 "There was a problem communicating with the secure web proxy server (HTTPS).

Can someone please help?

This is my NSURLConnection code snippet:

NSURL *url = [NSURL URLWithString:notePicUrl];

            NSURLRequest *urlRequest = [NSURLRequest  requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];
            //NSOperationQueue *queue = [[NSOperationQueue alloc] init];

            NSURLConnection* _connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:NO];
            self.port = [NSPort port];                                                                                                       
            self.runLoop = [NSRunLoop currentRunLoop];                                                                                       
            [self.runLoop addPort:self.port forMode:NSDefaultRunLoopMode];
            [_connection scheduleInRunLoop:self.runLoop forMode:NSDefaultRunLoopMode];
            [_connection start];
            while (self.finished != YES ) {
                [self.runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 0.1]]; 
            }
            [self.runLoop removePort:[self port] forMode:NSDefaultRunLoopMode];
            [_connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

And the NSURLConnectionDelegate methods (not yet implemented ; just testing to first see if it works) ...

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
    NSLog(@"didReceiveResponse");
    //_data = [[NSMutableData alloc] init]; // _data being an ivar
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    NSLog(@"didReceiveData");
    //[_data appendData:data];
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"didFailWithError %@", [error description]);
    // Handle the error properly
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    NSLog(@"connectionDidFinishLoading");
    //[self handleDownloadedData]; // Deal with the data
}

回答1:


I just fixed this ; I tried using the simple code line:

[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];

And then handling the data in the Delegate methods. Earlier, it was attempting to run in a separate thread. this works just fine & is asynchronous.



来源:https://stackoverflow.com/questions/12884753/nsurlconnection-sendasynchronousrequest-communicating-with-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!