基于ASIHTTPRequest封装的HttpClient

ASIHTTPRequest作为一个比较知名的http访问库本身功能比较强大,在项目开发过程中,如果每个请求,都要使用ASIHTTPRequest来写,有以下几个弊端:

(1)繁琐,无封装性。

(2)如果直接Synchronous方法,阻塞UI,而使用异步的Asynchronous,则要写很多委托,也是非常的麻烦.

(3)http请求基本上是给一个请求,返回一个请求结果,直接使用ASIHTTPRequest还无法做到,所以需要有一个比较好的封装。

基于以上的三个原因,做了一个封装

(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)实现文件

 

//
//  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)测试项目

      测试项目,以获取网站的html的源码来测试的(get方法),post方法还没有测试(如果项目中需要用到,最好测试下,应该不难)。

   

 

项目源码下载:http://files.cnblogs.com/likwo/Http-ClientDemo.zip

你可能感兴趣的