NSURLConnection sendAsynchronousRequest-与HTTPS通信 [英] NSURLConnection sendAsynchronousRequest - communicating with HTTPS
问题描述
我正在使用iPhone应用程序,该应用程序从URL提取图像.我使用的是"NSData dataWithContentsOfUrl]&工作正常.但是,正如您可能已经猜到的那样,请求是同步的.
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.
我希望请求是异步的.因此,我尝试使用NSURLConnection的sendAsynchronousRequest()调用.但这会在方法'didFailWithError'中返回以下错误:
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代码= 310出现问题与安全的Web代理服务器(HTTPS)通信.
Error Domain=kCFErrorDomainCFNetwork Code=310 "There was a problem communicating with the secure web proxy server (HTTPS).
有人可以帮忙吗?
这是我的NSURLConnection代码段:
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];
还有NSURLConnectionDelegate方法(尚未实现;仅需测试以首先查看其是否有效)...
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
}
推荐答案
我刚刚解决了这个问题;我尝试使用简单的代码行:
I just fixed this ; I tried using the simple code line:
[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
然后在Delegate方法中处理数据.此前,它试图在单独的线程中运行.这可以正常工作&是异步的.
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.
这篇关于NSURLConnection sendAsynchronousRequest-与HTTPS通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!