'get'에 해당되는 글 2건

  1. 2012.10.05 [iPhone] NSString URL 인코딩/디코딩
  2. 2012.05.29 iPhone NSURLRequest 사용법
iOS2012. 10. 5. 11:13

HTTP(GET, POST) 프로토콜을 이용하여 서버와의 통신을 할때는 문자열의 인코딩을 신경쓰게 될 수 밖에 없죠.

기존의 Ajax와 같은 웹 프로그래밍에서는 URLEncode, URLDecode와 같은 것을 많이 사용하게 되는데요 비슷한게 있나 찾아보았습니다.

결론부터 말하면 있더군요. 잘됩니다.

    // 오리지널 메시지
   
NSString *original = @"Hello, Nice to meet you\nWelcome to my blog(http://theeye.pe.kr)";
   
   
// URL Encode
   
NSString *escaped = [original stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   
NSLog(@"escaped string :\n%@", escaped);
   
   
// URL Decode
   
NSString *decoded = [escaped stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   
NSLog(@"decoded string :\n%@", decoded);

2009-04-03 12:20:58.721 URLEncodeExample[342:20b] escaped string :
Hello,%20Nice%20to%20meet%20you%0AWelcome%20to%20my%20blog(http://theeye.pe.kr)
2009-04-03 12:20:58.723 URLEncodeExample[342:20b] decoded string :
Hello, Nice to meet you
Welcome to my blog(http://theeye.pe.kr)

'iOS' 카테고리의 다른 글

Home, Temporary, Documents, Cache Directory Path 얻기  (0) 2012.10.05
[iPhone] Files and Networking  (0) 2012.10.05
Cycript 소개  (0) 2012.10.04
iOS - OTA ( Over the Air AdHoc )  (1) 2012.10.04
UITextField 키보드 간단하게 내리기  (0) 2012.10.04
Posted by 다오나무
영삼이의 IT정보2012. 5. 29. 15:14

/* 

아래 같은 방식으로 smsURL이라는 주소로 post 형식으로 데이터를 감싸 전송이 가능해진다.

*/

- (void)connectToServer

{

NSString *smsURL = @"http://www.google.co.kr";

NSMutableURLRequest *request = [[[NSMutableURLRequest allocinitautorelease];

NSString *post = [NSString stringWithFormat:@"password=%@&id=%@",@"password",@"id"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

[request setURL:[NSURL URLWithString:smsURL]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"Mozilla/4.0 (compatible;)" forHTTPHeaderField:@"User-Agent"];

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

[request setHTTPBody:postData];


[NSURLConnection connectionWithRequest:request delegate:self ];

}


/*

아래 Delegate에서는 HTML이 처리되고 난 후 받는 데이터를 얻을 수 있다.

*/

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

{

NSString *returnString = [[NSString allocinitWithData:data encoding:NSUTF8StringEncoding];

NSLog(returnString);

}


/* 

아래 Delegate를 이용하면 post를 보낸 후 쿠키를 얻을 수 있다.

*/

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse

{

NSHTTPCookie *cookie;

int i=0;

for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStoragecookies])

{

NSLog([cookie description]);

}

}

Posted by 다오나무