asihttp 源码分析

一:发起一个同步请求

 

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 队列中,在这里并没有看到该变量的初始化,那么它是在哪里被初始化的呢??

//在程序运行过程中,它会在你程序中每个类调用一次initialize。这个调用的时间发生在你的类接收到消息之前,但是在它的超类接收到initialize之后。

+ (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文件中自行查找。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值