// create the URL NSURL *postURL = [NSURL URLWithString:@"http://twitpic.com/api/upload"];
// create the connection NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL
// change type to POST (default is GET) [postRequest setHTTPMethod:@"POST"];
// just some random text that will never occur in the body NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary]; // set header [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
// create data NSMutableData *postBody = [NSMutableData data];
NSString *username = @"username"; NSString *password = @"password"; NSString *message = @"Testing TwitPic Upload for Blog Post";
// username part [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[username dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// password part [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[password dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// message part [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[message dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// media part [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Disposition: form-data; name=\"media\"; filename=\"dummy.jpg\"\r\n"dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// get the image data from main bundle directly into NSData object NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"]; NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
// add it to body [postBody appendData:imageData]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary]dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post [postRequest setHTTPBody:postBody];
// Request를 사용하여 실제 연결을 시도하는 NSURLConnection 인스턴스 생성 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
// 정상적으로 연결이 되었다면 if(connection) { NSLog(@"<< 요청 성공(POST) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", nil);
// 데이터를 전송받을 멤버 변수 초기화 receivedData = [[NSMutableData alloc] init]; return YES; }
NSLog(@"<< 요청 실패(POST) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", nil);
return NO; |
'Developer > iOS' 카테고리의 다른 글
[IPhone] TableView 터치시 파란색을 다른색으로 바꾸기 (1) | 2011.01.19 |
---|---|
[IPhone] TabBarController의 TabBar(hidden) 숨기기. (1) | 2011.01.18 |
[IPhone] 아이폰에서 주소록 가져오기. (4) | 2011.01.12 |
[IPhone] SMS보내기 (1) | 2011.01.12 |
[IPhone]UIAlertView(경고창) 띄우는 방법과 경고창이 여러개일 때, 이벤트 구별하는 방법 (2) | 2011.01.12 |