基於ASIHTTPRequest封裝的HttpClient

ASIHTTPRequest做爲一個比較知名的http訪問庫自己功能比較強大,在項目開發過程當中,若是每一個請求,都要使用ASIHTTPRequest來寫,有如下幾個弊端:html

(1)繁瑣,無封裝性。異步

(2)若是直接Synchronous方法,阻塞UI,而使用異步的Asynchronous,則要寫不少委託,也是很是的麻煩.oop

(3)http請求基本上是給一個請求,返回一個請求結果,直接使用ASIHTTPRequest還沒法作到,因此須要有一個比較好的封裝。post

基於以上的三個緣由,作了一個封裝測試

(1)頭文件網站

//
//  HttpClient.h
//
//  Created by likwo on 11-9-7.
// blog http://www.cnblogs.com/likwo
//  Copyright 2013年 . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ASIHTTPRequestDelegate.h"
#import "ASIHTTPRequest.h"

@interface HttpClient : NSObject <ASIHTTPRequestDelegate>
{
    BOOL isRequestFinish;
    NSMutableDictionary *postData;
}

@property (nonatomic, retain) NSMutableDictionary *postData;
@property (nonatomic, assign) BOOL isRequestFinish;
@property (nonatomic, assign) NSInteger timeOutSecond;
@property (nonatomic, retain)ASIHTTPRequest *request;


-(NSString *)get:(NSString *)url  error:(NSError **)error;

-(NSString *)post:(NSString *)url  postData:(NSDictionary *)data error:(NSError **)error;

- (void)cancel;

@end

 (2)實現文件ui

 

//
//  HttpClient.m
//  blog http://www.cnblogs.com/likwo
//  Copyright 2013年 . All rights reserved.
//

#import "HttpClient.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@interface HttpClient()

@end

@implementation HttpClient
@synthesize isRequestFinish;
@synthesize postData;

-(void)dealloc
{
    self.postData = nil;
    [self.request release];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    
    if (self) {
        self.timeOutSecond = 10;
    }
    return self;
}

-(NSData *)get:(NSString *)url method:(NSString *)method body:(NSString *)body error:(NSError **)error
{
    return [self.request responseData];
}


-(NSString *)get:(NSString *)url error:(NSError **)error;
{

#ifdef DEBUG
    NSLog(@"request get url %@",url);
#endif
    
    NSURL *nsUrl = [NSURL URLWithString:url];
    self.request = nil;
    
    self.request = [ASIHTTPRequest requestWithURL:nsUrl];
    [self.request setTimeOutSeconds:self.timeOutSecond];
    self.request.delegate = self;
    self.isRequestFinish = NO;
    
    [self.request startAsynchronous];
    
    while( !self.isRequestFinish)
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    
    if ([self.request error])
    {
        *error = [self.request error];
        
        return nil;
    }
    
    NSData *responseData = [self.request responseData];
    
    NSString *retStr = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
    
    if (retStr == nil)
    {
         retStr =  [[NSString alloc] initWithData:responseData encoding:NSUnicodeStringEncoding]; 
    }
    
#ifdef DEBUG
    NSLog(@"str %@",retStr);
#endif
    return retStr;
}


-(NSString *)post:(NSString *)url  postData:(NSDictionary *)data error:(NSError **)error;
{
    
#ifdef DEBUG
    NSLog(@"request post url %@",url);
#endif
    
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:nsUrl];
    [request setDelegate:self];
    
    for(id key in [data allKeys])
    {
        [request setPostValue:[self.postData objectForKey:key] forKey:(NSString *)key];
    }
    
    [request buildPostBody];
    
#ifdef DEBUG
    NSString * str = [[[NSString alloc] initWithData:[request postBody] encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"str %@",str);
#endif
    
    [request startAsynchronous];
    
    self.isRequestFinish = NO;
    while( !self.isRequestFinish )
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    
    if ([request error])
    {
        *error = [request error];
        
        return nil;
    }
    
    return [[[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding] autorelease];
    
    return nil;
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   self.isRequestFinish = YES;
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    self.isRequestFinish = YES;
}

- (void)cancel
{
    [self.request cancel];
}

@end

(3)測試項目atom

      測試項目,以獲取網站的html的源碼來測試的(get方法),post方法尚未測試(若是項目中須要用到,最好測試下,應該不難)。url

   

 

項目源碼下載:http://files.cnblogs.com/likwo/Http-ClientDemo.zipspa

相關文章
相關標籤/搜索