본문 바로가기

Computer

iPhone http client API (NSURLConnection) - 2

지난회에 이어 이번에는 비동기식 구현에 대해 알아보도록 하겠습니다.


    위와 같은 메소드들이 있습니다.
- start 를 빼고는 모두 delegate 를 지정하게 됩니다.

아래는 Delegate 입니다.

Connection Data and Responses



    모두 데이터를 보내고, 받는데 사용되는 Delegate 로 보입니다.

이를 토대로 Wrapper 클래스를 만들어보도록 하겠습니다.


HTTPClient.h

@interface HTTPClient : NSObject{

id<HTTPClientDelegate_delegate;

NSMutableData *receivedData;

NSHTTPURLResponse *response;

NSString *result;

NSString *_url;

NSString *_method;

NSData *_bodyData;

BOOL _isMultipart;

NSString *_boundary;

}



- (BOOL)requestUrl:(NSString *)url 

postMethodBodyWithNSDictionary:(NSDictionary *) postMethodBody;

- (BOOL)requestUrl:(NSString *)url 

getMethodBodyWithNSString:(NSString *) getMethodsBody;

- (BOOL)requestUrl:(NSString *)url 

postMethodBodyWithNSDictionary:(NSDictionary *) postMethodBody multiPartWithDictionary:(NSDictionary *) multiPartBody;


@property (nonatomicassignid<HTTPClientDelegate> delegate;

@property (nonatomicretain) NSMutableData *receivedData;

@property (nonatomicretain) NSHTTPURLResponse *response;

@property (nonatomicretain) NSString *result;


@end



@protocol HTTPClientDelegate <NSObject>


@optional

- (void)httpClient:(HTTPClient *)httpClient willSendRequest:

(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse;

- (void) httpClient:(HTTPClient*) httpClient didSendBodyData:

(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;

- (void) httpClient:(HTTPClient *) httpClient didReceiveData:(NSData *)data;

- (void) httpClientDidFinishLoading:(HTTPClient *)httpClient;

- (void) httpClient:(HTTPClient *) httpClient didFailWithError:(NSError *)error;

- (void) httpClient:(HTTPClient *) httpClient 

didReceiveResponse:(NSURLResponse *)response;


@end




위의 코드는 HTTPClient 와 HTTPClientDelegate 를 만든 것입니다.
Delegate 에 대해서는 iPhone Delegate 만들기에 정리해 놓았습니다.

- (BOOL)requestUrl:(NSString *)url 

postMethodBodyWithNSDictionary:(NSDictionary *) postMethodBody;

- (BOOL)requestUrl:(NSString *)url 

getMethodBodyWithNSString:(NSString *) getMethodsBody;

- (BOOL)requestUrl:(NSString *)url 

postMethodBodyWithNSDictionary:(NSDictionary *) postMethodBody multiPartWithDictionary:(NSDictionary *) multiPartBody;


이런 메소드 들로 GET, POST, Multipart 리퀘스트를 만들 수 있습니다.

id<HTTPClientDelegate_delegate;

Delegate Setter 를 만들어서 HTTPClientDelegate 에서 발생하는 이벤트를 위임받을 수도 있습니다.

@synthesize _url, _method, _bodyData, _isMultipart, _boundary;

@synthesize receivedData, response, result;

@synthesize delegate = _delegate;

@synthesize target, selector;


- (BOOL) requestUrl:(NSString *)url httpMethod:(NSString *)method       

bodyData:(NSData *)bodyData isMultipart:(BOOL)isMultipart boundary:(NSString*)boundary{


_url = url;

_method = method;

_bodyData = bodyData;

_isMultipart = isMultipart;

_boundary = boundary;


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]

cachePolicy:NSURLRequestUseProtocolCachePolicy

timeoutInterval:TIMEOUT_INTERVAL];

//Header Set

if (isMultipart) {

[request addValue:

[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

else {

[request addValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];

}


[request addValue:USER_AGENT forHTTPHeaderField:@"User-Agent"];

[request setHTTPMethod:method];

if (bodyData) {

[request setValue:[NSString stringWithFormat:@"%d",[bodyData length]]forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:bodyData];

}

NSURLConnection *connection = [[[NSURLConnection alloc]

initWithRequest:request delegate:self]autorelease];


if(connection){

receivedData = [[NSMutableData allocinit];

return YES;

}else {

return NO;

}

}






- (NSURLRequest *)connection:(NSURLConnection *)connection

 willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse{


if ([_delegate respondsToSelector:@selector(

httpClient:willSendRequest:redirectResponse:)]) {

[_delegate cafeHTTPClient:self willSendRequest:request

redirectResponse:redirectResponse];

}

return request;

}



- (void)connection:(NSURLConnection *)connection 

didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {


if ([_delegate respondsToSelector:@selector(

:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:)]) {

[_delegate cafeHTTPClient:self didSendBodyData:bytesWritten

totalBytesWritten:totalBytesWrittentotalBytesExpectedToWrite:totalBytesExpectedToWrite];

}

}


- (void)connection:(NSURLConnection *)connection

 didReceiveResponse:(NSURLResponse *)response{

if ([_delegate respondsToSelector:@selector(

httpClient:didReceiveResponse:)]) {

[_delegate httplient:self didReceiveResponse:response];

}

[receivedData setLength:0];

self.response = (NSHTTPURLResponse*) response;

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

if ([_delegate respondsToSelector:@selector(httpClient:didReceiveData:)]) {

[_delegate cafeHTTPClient:self didReceiveData:data];

}

[receivedData appendData:data];

}


- (void)connection:(NSURLConnection *)connection 

didFailWithError:(NSError *)error{

if ([_delegate respondsToSelector:@selector(

httpClient:didFailWithError:)]) {

[_delegate cafeHTTPClient:self didFailWithError:error];

}

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

if ([_delegate respondsToSelector:@selector(httpClientDidFinishLoading:)]) {

[_delegate cafeHTTPClientDidFinishLoading:self];

}

result = [[NSString allocinitWithData:receivedData

encoding:NSUTF8StringEncoding];

}



'Computer' 카테고리의 다른 글

iPhone Mobile Provisioning Profiles  (0) 2010.06.01
iPhone 3G , Wi-Fi 검사하기  (0) 2010.06.01
iPhone http client API (NSURLConnection) - 1  (0) 2010.05.31
iPhone user Agent  (0) 2010.05.25
iPhone - Delegate 만들기 - 2  (0) 2010.05.25