Loading Data Asynchronously
- 위와 같은 메소드들이 있습니다.
Connection Data and Responses
- connection:willCacheResponse:
delegate method– connection:didReceiveResponse:
delegate method– connection:didReceiveData:
delegate method– connection:didSendBodyData:totalBytesWritten:
totalBytesExpectedToWrite:
delegate method– connection:willSendRequest:redirectResponse:
delegate method
- 모두 데이터를 보내고, 받는데 사용되는 Delegate 로 보입니다.
@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 (nonatomic, assign) id<HTTPClientDelegate> delegate;
@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSHTTPURLResponse *response;
@property (nonatomic, retain) 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
- (BOOL)requestUrl:(NSString *)url
postMethodBodyWithNSDictionary:(NSDictionary *) postMethodBody;
- (BOOL)requestUrl:(NSString *)url
getMethodBodyWithNSString:(NSString *) getMethodsBody;
- (BOOL)requestUrl:(NSString *)url
postMethodBodyWithNSDictionary:(NSDictionary *) postMethodBody multiPartWithDictionary:(NSDictionary *) multiPartBody;
@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 alloc] init];
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 alloc] initWithData: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 |