2011년 5월 30일 월요일

iphone http Request

1. Sync 



NSURL *url = [NSURL URLWithString:kURLPath]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
NSURLResponse *response; 
NSError *error = nil; 
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
if(data == nil) { NSLog(@”%@”, [error description]);
}


2. ASync 

NSURL *url = [NSURL URLWithString:kURLString]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
 [connection start];

NSUrlConnectionDelegate  사용




NSURLConnectionDelegate Header 응답
Body 전송
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse { response = [aResponse retain];
if ([response expectedContentLength] < 0) { [connection cancel];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)aData {
// 다운로드한 데이터를 추가 [receiveData appendData:aData];
}


NSURLConnectionDelegate
전송 완료
오류 처리
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
UIImage *image = [UIImage imageWithData:receiveData] [self.imageView setImage:image];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error"
[alert show]; [alert release];
}
message:[error description] delegate:self
cancelButtonTitle:@"Close" otherButtonTitles:nil];





ASIHTTPRequest
HTTP 요청과 응답 관련 라이브러리 간편한 사용성 보안 및 인증 지원 Queue를 활용한 다중 다운로드 지원
http://allseeing-i.com/ASIHTTPRequest/How-to-use


댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.