一:发起一个同步请求
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request startSynchronous];
首先看一下ASIHTTPRequest 类
@interface ASIHTTPRequest : NSOperation <NSCopying>
该类是NSOperation 的子类,所以该类的实例可以添加进 NSOperationQueue 队列中进行异步的请求。
发起synshronous请求的方法
- - (void)startSynchronous
- {
- #if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING
- NSLog(@"Starting synchronous request %@",self);
- #endif
- [self setSynchronous:YES];
- [self setRunLoopMode:ASIHTTPRequestRunLoopMode];
- [self setInProgress:YES];
-
- if (![self isCancelled] && ![self complete]) {
- [self main];
- while (!complete) {
- [[NSRunLoop currentRunLoop] runMode:[self runLoopMode] beforeDate:[NSDate distantFuture]];
- }
- }
-
- [self setInProgress:NO];
- }
直接调用了[self main] 。
并且把runloopmode 设置为 ASIHTTPRequestRunLoopMode。
二:发起一个异步请求
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startAsynchronous];
startAsynchronous方法的实际内容为:
- - (void)startAsynchronous
-
- [sharedQueue addOperation:self];
request被加入到sharedQueue 队列中,在这里并没有看到该变量的初始化,那么它是在哪里被初始化的呢??
-
-
- + (void)initialize
- {
- if (self == [ASIHTTPRequest class]) {
- persistentConnectionsPool = [[NSMutableArray alloc] init];
- connectionsLock = [[NSRecursiveLock alloc] init];
- progressLock = [[NSRecursiveLock alloc] init];
- bandwidthThrottlingLock = [[NSLock alloc] init];
- sessionCookiesLock = [[NSRecursiveLock alloc] init];
- sessionCredentialsLock = [[NSRecursiveLock alloc] init];
- delegateAuthenticationLock = [[NSRecursiveLock alloc] init];
- bandwidthUsageTracker = [[NSMutableArray alloc] initWithCapacity:5];
- ASIRequestTimedOutError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIRequestTimedOutErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request timed out",NSLocalizedDescriptionKey,nil]];
- ASIAuthenticationError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIAuthenticationErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Authentication needed",NSLocalizedDescriptionKey,nil]];
- ASIRequestCancelledError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIRequestCancelledErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request was cancelled",NSLocalizedDescriptionKey,nil]];
- ASIUnableToCreateRequestError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASIUnableToCreateRequestErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create request (bad url?)",NSLocalizedDescriptionKey,nil]];
- ASITooMuchRedirectionError = [[NSError alloc] initWithDomain:NetworkRequestErrorDomain code:ASITooMuchRedirectionErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request failed because it redirected too many times",NSLocalizedDescriptionKey,nil]];
- sharedQueue = [[NSOperationQueue alloc] init];
- [sharedQueue setMaxConcurrentOperationCount:4];
-
- }
- }
就是这个方法了。
并且sharedQueue的类型为NSOperationQueue 。并不是ASINetworkQueue,关于ASINetworkQueue类后面介绍
sharedQueue = [[NSOperationQueue alloc] init];
[sharedQueue setMaxConcurrentOperationCount:4];
三:关于http的一些信息
+ (id)requestWithURL:(NSURL *)newURL;
+ (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache;
+ (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache andCachePolicy:(ASICachePolicy)policy;
以上三种ASIHTTPRequest 的构建都是通过调用- (id)initWithURL:(NSURL *)newURL 方法。
- - (id)initWithURL:(NSURL *)newURL
- {
- self = [self init];
- [self setRequestMethod:@"GET"];
- [self setRunLoopMode:NSDefaultRunLoopMode];
- [self setShouldAttemptPersistentConnection:YES];
- [self setPersistentConnectionTimeoutSeconds:60.0];
- [self setShouldPresentCredentialsBeforeChallenge:YES];
- [self setShouldRedirect:YES];
- [self setShowAccurateProgress:YES];
- [self setShouldResetDownloadProgress:YES];
- [self setShouldResetUploadProgress:YES];
- [self setAllowCompressedResponse:YES];
- [self setShouldWaitToInflateCompressedResponses:YES];
- [self setDefaultResponseEncoding:NSISOLatin1StringEncoding];
- [self setShouldPresentProxyAuthenticationDialog:YES];
-
- [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]];
- [self setUseSessionPersistence:YES];
- [self setUseCookiePersistence:YES];
- [self setValidatesSecureCertificate:YES];
- [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]];
- [self setDidStartSelector:@selector(requestStarted:)];
- [self setDidReceiveResponseHeadersSelector:@selector(request:didReceiveResponseHeaders:)];
- [self setWillRedirectSelector:@selector(request:willRedirectToURL:)];
- [self setDidFinishSelector:@selector(requestFinished:)];
- [self setDidFailSelector:@selector(requestFailed:)];
- [self setDidReceiveDataSelector:@selector(request:didReceiveData:)];
- [self setURL:newURL];
- [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]];
- [self setDownloadCache:[[self class] defaultCache]];
- return self;
- }
该方法设置一个http请求相关的属性,已经一些lock 的初始化。
四:下面看看实际联网的方法 main()
// Create the request
- (void)main{
}
改方法包含实际的请求逻辑,是整个的核心。
由于该方法过于庞大,在这里就不列出了,可以到ASIHTTPRequest.m文件中自行查找